工厂模式
var lev =function(){
return "hello world";
};
function Parent(){
var child=new Object();
child.name="孙悟空";
child.age=400;
child.lev=lev;
return child;
}
var x = Parent();
alert(x.name); alert(x.lev());
构造函数方式
var lev=function(){
return "hello,world";
};
function Parent(){
this.name="孙悟空";
this.age=400;
this.lev=lev;
}
var x=new Parent();
alter(x.name);alert(x.lev());
原型模式
var lev=function(){
return "hello world";
};
function Parent(){};
Parent.Prototype.name="孙悟空";
Parent.prototype.age=400;
Parent.prototype.lev=lev;
var x=new Parent();
alert(x.lev());
混合构造函数
function Parent(){
this.name="孙悟空";
this.age=400;
}
Parent.prototype.lev=function(){
return this.name;
}
var x =new Parent();
alert(x.lev());
动态原型方式
function Parent(){
this.name="孙悟空";
this.age=400;
};
if (typeof Parent._lev=="undefined"){
Parent.prototype.lev=function(){
return this.name;
}
Parent._lev=true;
}
var x=new Parent();
alert(x.lev());
json方式创建
var Parent={
name:"孙悟空",
age:400,
lev:function(){
return "hello world";
}
};
alert(Parent.lev());
原文地址:http://iammoon24.blog.51cto.com/2552968/1951725