创建对象
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>组合使用构造函数模式和原型模式</title> 6 </head> 7 <body> 8 <script> 9 function Person(name,age,job){ 10 this.name = name; 11 this.age = age; 12 this.job = job; 13 this.friend = [‘Amy‘,‘Bob‘]; 14 } 15 Person.prototype.sayName = function(){ 16 console.log(‘name:‘ + this.name); 17 } 18 var person1 = new Person(‘Kevin‘,23,‘Teacher‘); 19 var person2 = new Person(‘David‘,24,‘Doctor‘); 20 person1.sayName(); // name:Kevin 21 person1.friend.push(‘Cris‘); 22 console.log(‘person1.friend:‘ + person1.friend); // person1.friend:Amy,Bob,Cris 23 console.log(‘person2.friend:‘ + person2.friend); // person2.friend:Amy,Bob 24 console.log(person1.sayName == person2.sayName); // true 25 console.log(person1.friend == person2.friend); // false 26 </script> 27 </body> 28 </html>
继承
时间: 2024-10-28 21:16:09