1.原型链继承
Father.prototype.lastName = 'Jason';
function Father() {}
function Son() {}
// 此处实现继承
Son.prototype = new Father();
var son = new Son();
console.log(son.lastName) // Jason
2.伪经典继承(借用构造函数)
Father.prototype.sex = 'man';
function Father(name) {
this.name = name;
}
function Son(name, age) {
Father.call(this, name);
this.age = age;
}
Son.prototype = new Father();
var son = new Son();
console.log(sex) // man
3.寄生组合式继承(圣杯继承)
function inherit (Target, Original) {
function F () {}
F.prototype = Original.prototype;
Target.prototype = new F();
Object.defineProperty(Target.prototype, 'constructor', {
enumerable: false,
value: Target
});
}
function Father () {}
function Son () {}
inherit(Son, Father);
Father.prototype.lastName = "Jason";
var son = new Son();
console.log(son.lastName) // Jason
推荐使用最后一种