对象的封装,(即创建方法),工厂,构造,原型,混合方法

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

posted @ 2013-07-08 02:55  好记性还真不如烂笔头  阅读(211)  评论(0编辑  收藏  举报