js继承的多种方式

242 阅读1分钟

继承定义

子类继承父类的属性和方法

原型继承

function Par(){
    this.name = 100
}
Par.prototype.getName = function(){
    return this.name
}

function Chi(){

}
Chi.prototype = new Par()
let obj22  = new Chi()
console.log(obj22.getName())
console.log(obj22.name)

核心: 子类原型指向父类实例

class 继承

class Par{
    constructor(name){
        this.name = name
    }
    getName(){
        return this.name
    }
}
class Chi extends Par{
    constructor(name){
        super(name)
    }
    getChiName(){
        return super.getName()
    }
}
console.log(new Chi('qiang').getChiName())

未完待续....