/** * 组合继承 */function Super(name) { ???this.name = name || ‘‘;}Super.prototype.age = 23;function Sub(name) { ???Super.call(this, name); ???this.eat = function() { ???????return this.name + ‘eat‘; ???}}Sub.prototype = new Super();Sub.prototype.constructor = Subvar sub = new Sub(‘xiaoming‘);console.log(sub);
/** * 寄生组合继承 */function Super(name) { ???this.name = name || ‘‘;}Super.prototype.age = 23;function init(Sub, Sup) { ???var proto = Object(Sup.prototype); ???proto.constructor = Sub; ???Sub.prototype = proto;}function Sub(name) { ???Super.call(this, name); ???this.eat = function() { ???????return this.name + ‘eat‘; ???}}init(Sub, Super);// Sub.prototype = new Super();// Sub.prototype.constructor = Subvar sub = new Sub(‘xiaoming‘);console.log(sub);
1、原型链继承
2、构造函数继承
3、组合继承
其实就是结合了原型链继承和构造函数的继承
缺点:之类继承父类的过程需要两次调用父类的构造器,这其实很浪费。
4、组合寄生继承
优点:解决了组合继承的缺点
缺点:复杂,而且当你改变子类的prototype时,父类的prototype也会改变。
js 继承
原文地址:https://www.cnblogs.com/pengwenfan/p/9012913.html