码迷,mamicode.com
首页 > Web开发 > 详细

js原型二

时间:2016-05-20 13:23:51      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

 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)

 

js原型二

标签:

原文地址:http://www.cnblogs.com/bhan/p/5511595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!