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

动态原型

时间:2014-12-04 13:38:00      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   sp   on   div   log   cti   

原型中的方法是共享的,如果每次创建构造函数时不必在重新在原型中添加方法

function Person(name,age){

  this.name = name;

  this.age = age;

  //方法

  if(typeof this.sayName != "function")

  {

     Person.prototype.sayName = function(){

        alert();

      };

  }

}

这样在new Person时 sayName方法就不必每次都重新创建。

动态原型

标签:style   blog   io   color   sp   on   div   log   cti   

原文地址:http://www.cnblogs.com/lcw5945/p/4142477.html

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