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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值