---
title: "用面向对象的方式封装javascript(Animal.js ,Bird.js,People.js)"
author: "77321660"
date: "2009-05-26T14:01:22Z"
tags: []
slug: "javascript-animal-js-bird-js-people-js"
draft: false
lastmod: "2009-05-26T14:01:22Z"
id: 396277
url: "https://77321660.iteye.com/blog/396277"
---

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 的内容 ===========

&lt;HTML xmlns="[http://www.w3.org/1999/xhtml](http://www.w3.org/1999/xhtml)"&gt;  
&lt;HEAD&gt;  
&lt;meta http-equiv="Content-Type" content="text/html; charset=GBK" /&gt;  
&lt;meta name="author" CONTENT="DONG\_XUELIN"&gt;  
&lt;title&gt;采用对象方式封装javascript代码&lt;/title&gt;  
&lt;script type="text/javascript" src="Animal.js"&gt;&lt;/script&gt;  
&lt;script type="text/javascript" src="Bird.js"&gt;&lt;/script&gt;  
&lt;script type="text/javascript" src="People.js"&gt;&lt;/script&gt;  
&lt;style type="text/css"&gt;  
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;  
}  
&lt;/style&gt;  
&lt;/HEAD&gt;  
&lt;BODY&gt;

&lt;table id="theTable\_1" width="100%" border="0" cellspacing="0" cellpadding="0"&gt;  
&lt;tr&gt;  
&lt;td&gt;&nbsp;&lt;/td&gt;  
&lt;td align="left" width="20%"&gt;Animal weight：&lt;/td&gt;  
&lt;td align="left"&gt;&lt;input type="text" id="inputObj\_x\_id" name="inputObj\_x\_name" value="1" title="必须输入0-9999内的数字"/&gt;&lt;/td&gt;  
&lt;/tr&gt;  
&lt;tr&gt;  
&lt;td&gt;&nbsp;&lt;/td&gt;  
&lt;td align="left" width="20%"&gt;Bird weight：&lt;/td&gt;  
&lt;td align="left"&gt;&lt;input type="text" id="inputObj\_y\_id" name="inputObj\_y\_name" value="10" title="必须输入0-9999内的数字"/&gt;&lt;/td&gt;  
&lt;/tr&gt;  
&lt;tr&gt;  
&lt;td&gt;&nbsp;&lt;/td&gt;  
&lt;td align="left" width="20%"&gt;People weight：&lt;/td&gt;  
&lt;td align="left"&gt;&lt;input type="text" id="inputObj\_z\_id" name="inputObj\_z\_name" value="100" title="必须输入0-9999内的数字"/&gt;&lt;/td&gt;  
&lt;/tr&gt;  
&lt;tr&gt;  
&lt;td colspan="3"&gt;  
&lt;input type="button" class="button" onclick="viewResult() ;" value="查看结果" /&gt;  
&lt;input type="button" class="button" onclick="removeConsole();" value="清除控制台" /&gt;  
&lt;/td&gt;  
&lt;/tr&gt;  
&lt;/table&gt;  
&lt;br&gt;  
&lt;br&gt;  
&lt;table id="theTable\_2" width="100%" border="0" cellspacing="0" cellpadding="0"&gt;  
&lt;tr class="headRow"&gt;  
&lt;td&gt;console\[信息台]&lt;/td&gt;  
&lt;/tr&gt;  
&lt;/table&gt;  
&lt;br&gt;  
&lt;/BODY&gt;  
&lt;div id="console\_id" style="height:150;overflow-y:auto;width:100%;"&gt;&lt;/div&gt;  
&lt;SCRIPT language="javascript"&gt;  
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("&lt;span style='padding-bottom:4px;font-size:12px;color:" color "'&gt;");  
newChild.innerText = message;  
consoleObj.appendChild( newChild );  
newChild = document.createElement("&lt;br&gt;");  
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"); 

}

&lt;/SCRIPT&gt;  
&lt;/HTML&gt;

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