AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
## 组合继承 组合继承 : 借用构造函数 + 原型继承 ``` function Person(name, age, gender) { this.name = name; this.age = age; this.gender = gender; } Person.prototype.sayHi = function () { console.log('大家好,我是' + this.name); } //子类型 function Student(name, age, gender, score) { //借用构造函数 Person.call(this, name, age, gender); this.score = score; } Student.prototype = new Person(); Student.prototype.constructor = Student; //学成特有的方法 Student.prototype.exam = function () { console.log('考试'); } var s1 = new Student('jack', 18, '男', 100); console.log(s1); function Teacher(name, age, gender, salary) { Person.call(this, name, age, gender); this.salary = salary; } //通过原型让子类型继承父类型的方法 Teacher.prototype = new Person(); Teacher.prototype.constructor = Teacher; var t1 = new Teacher('milan', 45, '女', 10000); console.log(t1); ```