日期:2014-05-16 浏览次数:20404 次
function Something(name,age){ this.name=name; //这里的属性,每个实例会复制一份,是自己的 this.age=age; } Something.prototype={ //这里的方法是共享的 sayName:function(){ alert(this.name) }, sayAge:function(){ alert(this.age); } } var s1=new Something('bill','30'); s1.sayName(); s1.sayAge(); var s2=new Something('jobs','40'); s2.sayName(); s2.sayAge();
------解决方案--------------------
function MAPS(property1, property2, property3, .......) {
this.property1 = property1;
this.property2 = property2;
this.property3 = property3;
..........................
this.method1 = function() {相同的操作1};
this.method2 = function() {相同的操作2};
this.method3 = function() {相同的操作3};
}
var MapObject = new MAPS(property1, property2, property3, .......);//生成实例
MapObject.method1; //调用方法
------解决方案--------------------
function object(){
this.config = {
attr1:1,
attr2:2
}
this.method = {
method1:function(){},
method2:function(){}
}
return {}
}