面向对象-构造函数-优化-方案1


 1  //优化前
 2     function Person (name,age){
 3         this.name=name;
 4         this.age=age;
 5         this.run=function () {
 6             console.log('pao');
 7         }
 8     }
 9     var p=new Person('sz',18);
10     var p2=new Person('zs',19);
11     console.log(p.run == p2.run);//返回false
12 //优化后
13     var runfuctin=function () {
14         console.log('pao');
15     }
16 
17     function Person (name,age){
18         this.name=name;
19         this.age=age;
20         this.run=runfuctin;
21     }
22     var p=new Person('sz',18);
23     var p2=new Person('zs',19);
24     console.log(p.run == p2.run);//返回true

 

 
posted @ 2017-05-07 13:24  浅悠落0521  阅读(246)  评论(0编辑  收藏  举报