实现类的继承
类的继承在几年前是重点内容,有n种继承方式各有优劣,es6普及后越来越不重要,那么多种写法有点『回字有四样写法』的意思,如果还想深入理解的去看红宝书即可,我们目前只实现一种最理想的继承方式。
1 function Parent(name) { 2 this.parent = name 3 } 4 Parent.prototype.say = function() { 5 console.log(`${this.parent}: 你打篮球的样子像kunkun`) 6 } 7 function Child(name, parent) { 8 // 将父类的构造函数绑定在子类上 9 Parent.call(this, parent) 10 this.child = name 11 } 12 /*13 1. 这一步不用Child.prototype =Parent.prototype的原因是怕共享内存,修改父类原型对象就会影响子类 14 2. 不用Child.prototype = new Parent()的原因是会调用2次父类的构造方法(另一次是call),会存在一份多余的父类实例属性 15 3. Object.create是创建了父类原型的副本,与父类原型完全隔离 16 */ 17 Child.prototype = Object.create(Parent.prototype); 18 Child.prototype.say = function() { 19 console.log(`${this.parent}好,我是练习时长两年半的${this.child}`); 20 } 21 // 注意记得把子类的构造指向子类本身 22 Child.prototype.constructor = Child; 23 var parent = new Parent('father'); 24 parent.say() // father: 你打篮球的样子像kunkun 25 var child = new Child('cxk', 'father'); 26 child.say() // father好,我是练习时长两年半的cxk

浙公网安备 33010602011771号