prototype 用法

prototype使得js面向对象
使用了prototype之后,使用它里面的属性或者函数 需要new出一个对象才可以使用。
否则不使用prototype,直接向对象注入

 

 1 function Person(){
 2     
 3 }
 4 Person.prototype.a = 5;
 5 Person.prototype.b = function(){
 6     alert("bbb");
 7 }
 8 var p = new Person();
 9 alert(p.a);
10 p.b();
11 alert(p.constructor);
12 
13 function SupPerson(){
14     
15 }
16 //SupPerson.prototype = Person.prototype;
17 SupPerson.prototype = p;
18 var sp = new SupPerson();
19 sp.b();

 

posted on 2014-06-11 14:17  wf110  阅读(482)  评论(0编辑  收藏  举报