继承

实例化对象继承

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);

Untitled

利用 call/apply 借用构造函数

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);

Untitled

Student.prototype = Teacher.prototype

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);

Untitled

🌈 圣杯模式继承(圣杯中间的部位就是连接两头的中间件)

方案:

  1. 创建一个空的构造函数
  2. 使其继承父类构造函数的原型