继承

40 阅读1分钟

es6 extends继承

原型式继承

构造函数继承

组合式继承

一 、

es6 extends继承

<script>
// es6继承
class Parent{
    constructor(){
        this.age=18;
    }
}

class Child extends Parent{
    constructor(){
        super()
        this.name='张三'
    }
}

let child=new Child()
console.log(child.age,child.name) // 18   张三


</script>

二、原型式继承

// 原型链继承
function Parent() {
    this.age=50
}

function Child() {
    this.name='张三'
}

Child.prototype=new Parent();
let child =new Child()
console.log(child.name,child.age) // 张三 50

三、构造函数继承

// 构造函数
function Parent() {
    this.age=50;
}

function Child() {
    Parent.call(this)
    this.name='张三'
}

let child=new Child()
console.log(child.age,child.name)

四、组合式继承

// 构造函数
function Parent() {
    this.age=50;
}

function Child() {
    Parent.call(this)
    this.name='张三'
}
Child.prototype=new Parent();
let child=new Child()
console.log(child.age,child.name) // 50 张三