js继承的方式有哪些


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);

文章作者: iamfugui
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 iamfugui !
评论
  目录