1。工厂函数方法,不推荐:
function ex(color,size){
var obj = {};
obj.color = color;
obj.size = size;
obj.say = function(){
alert("工厂方法");
}
}
var obj = ex("颜色值", "大小");
obj.color;
obj.say();
2.构造方法:
function ex(color, size){
this.color = color;
this.size = size;
this.say = function(){
alert("这是构造方法");
}
}
var obj = new ex("颜色", "大小");
obj.color;
obj.say();
3。原型模式
function ex(color,size){
this.color = color;
this.size = size;
this.say = function(){
alert("下面的就是原形的模式");
}
}
ex.prototype.name = "名字";
ex.prototype.display = function(){
alert("展示出来");
}
var obj = new ex("red", 100);
obj.color;
obj.display();
4。混合模式:(构造方法与原型模式结合)
function ex(color, size){
this.color = color;
this.size = size;
this.say = function(){
alert("这是混合模式");
}
ex.prototype.name = {"value", "值1"};
}
ex.prototype.display = function(){
alert("混合模式中的原型模式");
}
var obj = new ex("颜色值", 98);
obj.color;
obj.name.value