1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 7 <script type="text/javascript"> 8 9 /** 10 * 为了解决原型带来的所有问题,此时需要通过组合构造函数和原型来实现对象的创建 11 * 将属性在构造函数中定义,将方法在原型中定义 12 * 这种有效集合了两者的有点,是目前最为有效的一种方式 13 * @constructor 14 */ 15 function Person(name , age , friends){ 16 17 this.name = name; 18 this.age = age; 19 this.friends = friends; 20 } 21 Person.prototype = { 22 constructor : Person, 23 //方法在原型中定义 24 say : function(){ 25 console.info(this.name + "[" + this.friends + "]"); 26 } 27 } 28 //此时所有的属性都保存在自己的空间 29 var p1 = new Person("leon" , 23 , ["Ada" , "Chris"]); 30 p1.name = "John"; 31 p1.say(); //John[Ada,Chris] 32 p1.friends.push("Mike"); 33 p1.say(); //John[Ada,Chris,Mike] 34 var p2 = new Person("Ada" , 33 , ["Leon"]); 35 36 p2.say();//Ada[Leon] 37 38 39 </script> 40 41 </head> 42 <body> 43 44 </body> 45 </html>
原文:http://www.cnblogs.com/a757956132/p/5266177.html