首页 > 代码库 > 面向对象-构造函数-优化-方案1

面向对象-构造函数-优化-方案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

 

 

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