jsで継承を実現する方法、プロトタイプチェーン上の属性共有を防止する方法

656 ワード

スプリアスコンビネーション継承の使用
function super (Name){
    this.name =Name;
    this.sayName = function(){
        console.log(this.name)
    }
}
super.prototype.say = function(){
    console.log(this.name)
}
function sub (Age){
    super.call(this)
    this.age = Age
}
sub.prototype =Object.create(super.prototype)
sub.prototype.constructor = sub;


転載先:https://juejin.im/post/5aa4f96b518825555d46e1b5