码迷,mamicode.com
首页 > 其他好文 > 详细

动态原型

时间:2017-07-06 15:13:45      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:封装   代码   构造   lin   this   bsp   keyword   ace   构造函数   

动态原型模式将所有信息封装在了构造函数中,而通过构造函数中初始化原型(仅第一个对象实例化时初始化原型),这个可以通过判断该方法是否有效而选择是否需要初始化原型。

function Blog(name, url) {
  this.name = name;
  this.url = url;
 
  if (typeof this.alertInfo != ‘function‘) {
    // 这段代码只执行了一次
    alert(‘exe time‘);
    Blog.prototype.alertInfo = function() {
      alert(thia.name + this.url);
    }
  }
}
 
var blog = new Blog(‘wuyuchang‘, http://tools.***.net),
  blog2 = new Blog(‘wyc‘, ‘http:***.com‘);

}

动态原型

标签:封装   代码   构造   lin   this   bsp   keyword   ace   构造函数   

原文地址:http://www.cnblogs.com/guanguan-/p/7126226.html

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