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

相关文章:

  • 2021-12-30
  • 2021-07-31
  • 2021-12-25
  • 2022-12-23
  • 2021-07-11
  • 2023-03-07
  • 2021-09-21
猜你喜欢
  • 2021-08-22
  • 2022-12-23
  • 2021-12-01
  • 2021-11-21
  • 2021-06-19
  • 2021-06-22
  • 2018-05-23
相关资源
相似解决方案