1. class继承
class A {
constructor() {
this.age = 18;
}
}
class B extends A {
constructor() {
super();
this.name = '张三'
}
}
let son = new Child();
console.log(son);
2. 原型链继承
new A 放到 B 原型链上 ,new b 给新对象 , 那么新对象就能通过B原型链访问A的属性
function Parent() {
this.age = '18'
}
function Child() {
this.name = '张三'
}
Child.prototype = new Parent();
let son = new Child();
console.log(son);
3. 构造函数显示绑定(call)改变this
function Parent() {
this.age = '18'
}
function Child() {
Parent.call(this);
this.name = '张三'
}
let son = new Child();
console.log(son);