组合继承
关键:原型链继承+借用构造函数继承
缺点:1、使用组合继承时,父类构造函数会被调用两次,子类实例对象与子类的原型上会有相同的方法与属性,浪费内存。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| function Father(name) { this.name = name this.say = function () { console.log('hello,world'); } } Father.prototype.showName = function () { console.log(this.name); } function Son(name, age) { Father.call(this, name) this.age = age } Son.prototype = new Father() Son.prototype.constructor = Son Son.prototype.showAge = function () { console.log(this.age); } let p = new Son('刘逍', 20) console.log(p); p.showName() p.showAge()
|