1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| function Parent() { this.sayHello = function () { console.log("Hello"); } }
Parent.prototype.a = "我是父类prototype上的属性";
function Child() { Parent.call(this) }
var child1 = new Child(); var child2 = new Child();
console.log(child1.sayHello === child2.sayHello);
var parentObj = new Parent(); console.log(parentObj.a); console.log(child1.a)
|
优点:这种继承方式的好处是,原型属性不会被共享。
缺点:它不能继承父类prototype上的属性