//原型链继承
function Parent() { this.name = 'per'; } function Child() { this.age = 20; } Child.prototype = new Parent(); var child = new Child(); console.log(child.name + " " + child.age)//per 20
//构造函数继承
function Parent(age) { this.name = 'person'; this.age = age; } function Child(age) { Parent.call(this, age) } var child = new Child(20) console.log(child.name + " " + child.age)//per 20
// 组合继承
function Parent(age) { this.name = 'per'; this.age = age; } Parent.prototype.sayAge = function () { return this.name + ' age is ' + this.age; } function Child(age) { Parent.call(this, age) } Child.prototype = new Parent(); var child = new Child(21); console.log(child.sayAge())//per age is 21