组合继承:原型链+借助构造函数(callapply解决父类实例属性被共享)

    技术2022-07-16  73

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script> /** * 构造函数Person * @constructor */ function Person(name, pets) { this.name = name; this.pets = pets; } Person.prototype.run = function () { console.log('跑'); }; /** * 构造函数Student * @constructor */ // 组合继承:借助构造函数+原型链 function Student(num, name, pets) { // 注意: 一定要放在最前面 Person.call(this, name, pets); // 借调父类的构造函数 this.num = num; } // 1. 构造父类的实例 // 原型链继承 var p = new Person(); // 2. 并设置为子类的原型对象 Student.prototype = p; // 3.修复constructor指针即可 Student.prototype.constructor = Student; </script> <script> var stu = new Student('001', '张三', ['小花']); var stu2 = new Student('002', '李四', ['小茂']); console.log(stu); console.log(stu2); </script> </body> </html>
    Processed: 0.010, SQL: 9