js中如何实现继承,如何防止原型链上属性共享

使用寄生式组合继承

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

你可能感兴趣的:(js中如何实现继承,如何防止原型链上属性共享)