网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网页制作 > Javascript
网页制作:Dreamweaver教程,FrontPages教程,Javascript教程,HTML教程,CSS教程,心得技巧,DHTML教程,网页特效,Discuz!论坛
本月文章推荐
.javascript+css 标签显示方式的思.
.js读取本地excel文档数据.
.JavaScript仿Windows关机效果.
.javascript实现仿新浪信息提示效.
.使用JavaScript切换DOM元素显示状.
.document.onLoad的触发时间.
.JavaScript学习笔记 2-变量、字符.
.Javascript实例教程(4) 探测浏览.
.Javascript实例教程(11) 隐藏scr.
.JavaScript控制鼠标滑轮事件.
.js入门·循环与判断/利用函数的简.
.使主页呈现“飞舞”特效.
.一个非常实用的javascript读写Co.
.Javascript实例教程(20) OLE Aut.
.js入门实例の构造函数\方法的定义.
.HTML页面如何象asp一样接受参数.
.JAVASCRIPT中的window.location和.
.Javascript设计网页中的下拉菜单.
.Javascript实例教程(19) 使用HoT.
.javascript回车提交表单.

用面向对象的方式封装javascript代码

发表日期:2008-11-14


用面向对象的方式封装javascript 代码

  javascript 虽然是弱类型检查的脚本语言,可是它也有很多面向对象的特性,因此我们可以模仿java语言的抽象、继承 和封装 来处理javascript 代码。

  还是以例子来进行说明。在这里给出3个js 和一个用于测试的 html。

  1、Animal.js 的内容

  2、Bird.js 的内容

  3、People.js 的内容

  4、Test.html 的内容

 

===== 1 Animal.js 的内容 ===========

// * * * * * * * * * * * * * * * * * * * *
// *        Animal
// * desc:定义一个超类
// * time:2008-11-11
// * author:dxl
// *
// * Dependencies: null
// * * * * * * * * * * * * * * * * * * * /

//定义静态变量
Animal.CREATOR = "上帝";
Animal.HOME = "地球";

//构造器
function Animal(){}

//构造器
function Animal(weight){
 //初始化成员变量
    this.weight = weight;
}

//定义成员变量
Animal.prototype.weight = 0;//重量

//定义行为方法(公共方法)
Animal.prototype.eat = function (args){
 return "吃食";
};
//定义行为方法(公共方法)
Animal.prototype.move = function (args){
 return "行";
};

//定义行为方法(私有方法)
Animal.prototype._animalPrivateMethod = function (args){
    return  args.length;
};

=====        1 end    ====================

 

 

===== 2 Bird.js 的内容 ===========

// * * * * * * * * * * * * * * * * * * * *
// *        Bird
// * desc:定义Animal的子类
// * time:2008-11-11
// * author:dxl
// *
// * Dependencies: Animal.js
// * * * * * * * * * * * * * * * * * * * /

//定义静态变量
Bird.SciName = "鸟";

//构造器
function Bird(){}

//构造器
function Bird(weight){
 //初始化成员变量
    this.weight = weight;
}

//Bird 是 Animal 的子类, 继承
Bird.prototype = new Animal();

//定义行为方法(公共方法)
Bird.prototype.eat = function (args){
 return "吃生食";
};
//定义行为方法(公共方法)
Bird.prototype.move = function (args){
 return "飞行";
};

//定义行为方法(私有方法)
Animal.prototype._birdPrivateMethod = function (args){
    return  args.length;
};

=====       2 end    ====================

 

 

===== 3 People.js 的内容 ===========

// * * * * * * * * * * * * * * * * * * * *
// *        People
// * desc:定义一个Animal的子类
// * time:2008-11-11
// * author:dxl
// *
// * Dependencies: Animal.js
// * * * * * * * * * * * * * * * * * * * /

//定义静态变量
People.SciName = "人";

//构造器
function People(){}

//构造器
function People(weight){
 //初始化成员变量
    this.weight = weight;
}

//People 是 Animal 的子类, 继承
People.prototype = new Animal();

//定义行为方法(公共方法)
People.prototype.eat = function (args){
 return "吃熟食" ;
};
//定义行为方法(公共方法)
People.prototype.move = function (args){
 return "步行";
};

//定义行为方法(私有方法)
People.prototype._peoplePrivateMethod = function (args){
    return args.length;
};

=====         3 end    ====================

 

 

===== 4 Test.html 的内容 ===========

 <HTML xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
 <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
 <meta name="author" CONTENT="DONG_XUELIN">
 <title>采用对象方式封装javascript代码</title>
 <script type="text/javascript" src="Animal.js"></script>
 <script type="text/javascript" src="Bird.js"></script>
 <script type="text/javascript" src="People.js"></script>
 <style type="text/css">
  body{ background:#fff;}
  .button{
   background:#eee;
   border: #666688 1px solid;
   padding-right: 2px;
   padding-left: 2px;
   font-size: 12px;
   filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=#ffffff, EndColorStr=#c1c1ce);
   cursor: hand;
   color: black;
   padding-top: 2px;
   onmouseover:expression(onmouseover=function (){this.className='button_over'});
   onmouseout:expression(onmouseout=function (){this.className='button'});
   onmousedown:expression(onmousedown=function (){this.className='button_down'});
  }
  .button_over {
   background:#fff;
   border: #666688 1px solid;
   padding-right: 2px;
   padding-left: 2px;
   font-size: 12px;
   filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=#c1c1ce, EndColorStr=#ffffff); cursor: hand;
   color: black;
   padding-top: 2px;
  }
  .button_down{
   background:#fff;
   border: #666688 1px solid;
   padding-right: 2px;
   padding-left: 2px;
   font-size: 12px;
   filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=#c1c1cd, EndColorStr=#ffffff); cursor: hand;
   color: black;
   padding-top: 2px;
  }
 </style>
</HEAD>
<BODY>

 <table id="theTable_1" width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
   <td>&nbsp;</td>
   <td align="left" width="20%">Animal weight:</td>
   <td align="left"><input type="text" id="inputObj_x_id" name="inputObj_x_name" value="1" title="必须输入0-9999内的数字"/></td>
  </tr>
  <tr>
   <td>&nbsp;</td>
   <td align="left" width="20%">Bird weight:</td>
   <td align="left"><input type="text" id="inputObj_y_id" name="inputObj_y_name" value="10" title="必须输入0-9999内的数字"/></td>
  </tr>
  <tr>
   <td>&nbsp;</td>
   <td align="left" width="20%">People weight:</td>
   <td align="left"><input type="text" id="inputObj_z_id" name="inputObj_z_name" value="100" title="必须输入0-9999内的数字"/></td>
  </tr>
  <tr>
   <td colspan="3">
    <input type="button" class="button" onclick="viewResult() ;" value="查看结果" />
    <input type="button" class="button" onclick="removeConsole();" value="清除控制台" />
   </td>
  </tr>
 </table>
 <br>
 <br>
 <table id="theTable_2" width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr class="headRow">
   <td>console[信息台]</td>
  </tr>
 </table>
 <br>
</BODY>
<div id="console_id" style="height:150;overflow-y:auto;width:100%;"></div>
<SCRIPT language="javascript">
 var inputObj_x = document.getElementById("inputObj_x_id");//x input object
 var inputObj_y = document.getElementById("inputObj_y_id");//y input object
 var inputObj_z = document.getElementById("inputObj_z_id");//z input object
 var consoleObj = document.getElementById("console_id");//console div object

//打印信息到控制台
function printMsg2Console(message,color){
 if(message == 'undefined') return;
 if(!color) color = 'black';

 if(consoleObj == 'undefined')
  consoleObj = document.getElementById("console_id");

 var newChild = document.createElement("<span style='padding-bottom:4px;font-size:12px;color:" + color + "'>");
 newChild.innerText = message;
 consoleObj.appendChild( newChild );
 newChild = document.createElement("<br>");
 consoleObj.appendChild( newChild );
 newChild.scrollIntoView(true);
}

 //清除控制台的信息
 function removeConsole(){
  if(consoleObj == 'undefined')
   consoleObj = document.getElementById("console_id");

   consoleObj.innerHTML = "";
 }

 //查看 动物, 鸟, 人 对象的结果
 function viewResult(){
  //打印全局变量 以及 函数执行结果
  var x = inputObj_x.value;
  var y = inputObj_y.value;
  var z = inputObj_z.value;
 
  var animal = new Animal(x);
  var bird = new Bird(y);
  var people = new People(z);
 
  var result = "animal \tweight = " + animal.weight + "; eat = " + animal.eat() + "; move = " + animal.move()
   +  "\nbird \t\t\tweight = " + bird.weight + "; eat = " + bird.eat() + "; move = " + bird.move()
   +  "\npeople \tweight = " + people.weight + "; eat = " + people.eat() + "; move = " + people.move();

  printMsg2Console(result, "black");

 }

</SCRIPT>
</HTML>

=====        4 end   ====================

上一篇:JavaScript发送邮件 人气:691
下一篇:JavaScript与ActionScript通讯(ExternalInterface类) 人气:436
浏览全部javascript的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐