ES5实现继承---寄生组合式继承
核心思想: 创建一个中间对象,这个对象满足其隐式原型指向父类的显式原型,将这个对象赋值给子类的显式原型。
function createObj(o) { function F() {} F.prototype = o return new F() } function inherit(subType, superType) { subType.prototype = createObj(superType.prototype) Object.defineProperty(subType.prototype, 'constructor', { enumerable: false, configurable: true, writable: true, value: subType }) } // 实现Student继承自Person function Person() {} function Student() { // 属性继承 Person.call(this) } // 方法继承 inherit(Student, Person)