Professor.prototype = {
name: 'Mr.Zhang',
tSkill: 'Java'
}
function Professor() {}
var professor = new Professor();
Teacher.prototype = professor;
function Teacher() {
this.name = 'Mr.Wang';
this.mSkill = 'JS';
}
var teacher = new Teacher();
Student.prototype = teacher;
function Student() {
this.name = 'Mr.Li';
this.pSkill = 'HTML';
}
var student = new Student();
console.log(student);
call/apply
是通过改变 this 指向,来借用构造函数的属性和方法Teacher.prototype.wife = 'Ms.Liu';
function Teacher(name, mSkill) {
this.name = name;
this.mSkill = mSkill;
}
function Student(name, mSkill, age, major) {
Teacher.apply(this, [name, mSkill]);
this.age = age;
this.major = major;
}
var student = new Student('Mr.B', 'JS', 18, 'Computer');
console.log(student);
function Teacher() {
this.name = 'Mr.Wang';
this.mSkill = 'JS';
}
Teacher.prototype = {
pSkill: 'JQ'
}
var teacher = new Teacher();
Student.prototype = Teacher.prototype;
function Student() {
this.name = 'Mr.Li';
}
var student = new Student();
Student.prototype.name = 'student';
console.log(student);
console.log(Teacher.prototype);
方案: