<script language="javascript">
var myNameSpace = new Object(); //构造一个命名 空间myClass
//类的属性
myNameSpace.names="cnscn";
myNameSpace.age="30";
myNameSpace.ClassPersons=function(argName,argAge){ //构造类名,即构造函数
this.names=argName;
this.age=argAge;
}
myNameSpace.ClassPersons.prototype={//类方法的定义
//方法getName()
getName:function(){
alert(this.names);
}, //注意这里是"逗号"
//方法setName(argName)
setName:function(argName){
this.names=argName;
}
}
var obj=new myNameSpace.ClassPersons("yao", 23);
obj.getName();
obj.setName("hhhh");
obj.getName();
</script>
<script language="javascript">
var myClass = new Object();
myClass.names="cnscn";
myClass.age="30";
//定义类ClassPersons
myClass.ClassPersons=function(argName,argAge){
this.names=argName;
this.age=argAge;
}
myClass.ClassPersons.prototype={
getName:function(){
alert(this.names);
},
setName:function(argName){
this.names=argName;
}
}
//定义类ClassUsers
myClass.ClassUsers=function(argName,argAge){
this.names=argName;
this.age=argAge;
}
myClass.ClassUsers.prototype={
getName:function(){
alert(this.names);
},
setName:function(argName){
this.names=argName;
}
}
var obj=new myClass.ClassPersons("yao", 23);
obj.getName();
obj.setName("hhhh");
obj.getName();
var obj2=new myClass.ClassUsers("yao", 23);
obj2.getName();
obj2.setName("hhhh");
obj2.getName();
</script>
1)//namespace.js
var myClass = new Object();
myClass.names="cnscn";
myClass.age="30";
2)//ClassPersons.js
myClass.ClassPersons=function(argName,argAge){
this.names=argName;
this.age=argAge;
}
myClass.ClassPersons.prototype={
getName:function(){
alert(this.names);
},
setName:function(argName){
this.names=argName;
}
}
3)//ClassUsers.js
//定义类ClassUsers
myClass.ClassUsers=function(argName,argAge){
this.names=argName;
this.age=argAge;
}
myClass.ClassUsers.prototype={
getName:function(){
alert(this.names);
},
setName:function(argName){
this.names=argName;
}
}
.引用
<script type="text/javascript" src="namespace.js"></script>
<script type="text/javascript" src="ClassPersons.js"></script>
<script type="text/javascript" src="ClassUsers.js"></script>
<script language="javascript">
var obj=new myClass.ClassPersons("yao", 23);
obj.getName();
obj.setName("hhhh");
obj.getName();
var obj2=new myClass.ClassUsers("yao", 23);
obj2.getName();
obj2.setName("hhhh");
obj2.getName();
</script>
//构造函数
function Person(name){
//类变量
this.name = name;
this.age = 0;
this.gender = 0;
this.pets = new Array();
}
//类setName(name)的函数
Person.prototype.setName=function(name){
this.name=name;
}
//类getName(name)的函数
Person.prototype.getName=function(){
return this.name;
}
//类pushPetsArr(item)的函数
Person.prototype.pushPetsArr=function(item){
this.pets[this.pets.length] = item;
}
//构造函数
function Person(name){
//类变量
this.name = name;
this.age = 0;
this.gender = 0;
this.pets = new Array();
}
//类setName(name)的函数
Person.prototype = {
setName: function(name){
this.name=name;
},
//类getName(name)的函数
getName: function(){
return this.name;
},
//类pushPetsArr(item)的函数
pushPetsArr: function(item){
this.pets[this.pets.length] = item;
}
}