js原型二

 1 function Box(name,age){
 2 
 3    this.name = name;
 4 
 5    this.age = age;
 6 
 7    this.family = ['哥哥',‘姐姐’,‘妹妹’];
 8 
 9 }
10 
11 Box.prototype = {
12 
13    constructor:Box,
14 
15    run:functiong(){
16    return this.name+this.age+"运行中"
17 
18   }
19 }
20 var box1 = new Box("Lee",100);
21 
22 var box2 = new Box("Jack",200);

组合构造函数+原型模式

使用动态原型模式

 1 function Box(name,age){
 2    this.name = name;
 3    this.age = age;
 4   this.family = ['哥哥',‘姐姐’,‘妹妹’];
 5   if(typeof this.run !="function"){          //判断this.run是否存在
 6        Box.prototype.run=function(){
 7           return this.name+this.age+"运行中"
 8  }          
 9  }
10  }
11 //原型的初始化只要第一次初始化就可以了,没必要每次构造函数实例化的时候都初始化。
12  var box1 = new Box("Lee",100)
13  var box2 = new Box("Jack",200)
原文地址:https://www.cnblogs.com/bhan/p/5511595.html