一、ES6
class Parent {
constructor(){
this.age = 18;
}
}
class Child extends Parent {
constructor(){
super();
this.name = '嗨嗨嗨';
}
}
let o1 = new Child();
console.log(o1, o1.name, o1.age);
二、原型链继承
function Parent() {
this.age = 19;
}
function Child() {
this.name = '嗨嗨嗨';
}
Child.prototype = new Parent();
let o2 = new Child();
console.log(o2, o2.name, o2.age);
三、构造函数继承(call)
function Parent() {
this.age = 20;
}
fuunction Child() {
Parent.call(this);
this.name = '嗨嗨嗨';
}
let o3 = new Child();
console.log(o3, o3.name, o3.age);
四、组合式继承
function Parent() {
this.age = 21;
}
function Child() {
Parent.call(this);
this.name = '嗨嗨嗨';
}
Child.prototype = new Parent();
let o4 = new Child();
console.log(o4, o4.name, o4.age);