在引入class关键字之前,主要通过 原型链 和 call() 来实现类的继承 原型链 主要用来修改构造函数的对象原型 call() 用来修改 this 指向
借用父构造函数继承方法
// Father 实际上指向的是一个 构造函数Constructor // 1. 父构造函数 function Father(uname, age) { // this 指向父构造函数的对象实例 this.uname = uname this.age = age } // 2. 给 父构造函数 的 原型对象 声明方法 Father.prototype.money = function() { console.log('我是父类中的money()'); } // 3. 子构造函数 function Son(uname, age, score) { // this 指向子构造函数的对象实例 Father.call(this, uname, age) this.score = score } // 4. 此时创建的 Son 对象无法继承 父类中的方法 // var son = new Son('yzoo', 18, 100) // son.money() // Uncaught TypeError: son.money is not a function // 5. 更改子构造函数的对象原型 // 通过这种对象方式修改原型对象,可以避免 父原型对象 被修改 Son.prototype = new Father() // 6. 注意需要将 构造函数Constructor 指回原来的 Son构造函数 Son.prototype.constructor = Son // 7. 此时再创建 Son实例对象 就可以继承到 父类中的方法 var son = new Son('yzoo', 18, 100) son.money()