混入继承
关键:利用Object.assign的方法多个父类函数的原型拷贝给子类原型
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
| function Father(name) { this.name = name } Father.prototype.showName = function () { console.log(this.name); } function Mather(color) { this.color = color } Mather.prototype.showColor = function () { console.log(this.color); } function Son(name, color, age) { Father.call(this, name) Mather.call(this, color) this.age = age } Son.prototype = Object.create(Father.prototype) Object.assign(Son.prototype, Mather.prototype) const son = new Son('刘逍', 'red', 20) son.showColor()
|