javascript的prototype经典使用场景

时间:2015-05-14 17:59:00   收藏:0   阅读:160

prototype的经典使用场景就是为对象增加属性和方法,如给自定义的Man对象增加个姓名属性和语言方法:

function man() {
       this.age = "22";
   }
   var tom = new man();
   man.prototype.name = "tom";
   man.prototype.say = function () {
       alert("english");
   };
   alert(tom.name);
   tom.say();
 
结果:弹出”tom”,弹出”english”
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!