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

247 阅读1分钟

使用寄生式组合继承

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;