寫在最前面
繼承的發展史
- 傳統形式 –> 原型鏈
- 借用構造函數
- 不能繼承借用構造函數的原型
- 每次構造函數都要多走一個函數
- 共享原型
- 聖盃模式
原型鏈的繼承
A.prototype.name = "wu";
function A (){
}
var a = new A();
B.prototype = a;
function B (){
}
var b = new B();
C.prototype = b;
function C (){
}
var c = new C();
借用構造函數繼承
function Person(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
function Student(name,age,sex,grade){
Person.call(this,name,age,sex);
this.grade = grade;
}
var sutdent = new Student();
共享原型
Father.prototype.lastName = "tang"
function Father(){
}
function Son() {
}
Son.prototype = Father.prototype;
var son = new Son();
var father = new Father();
比如我在下面
Son.prototype.sex = "male";
son.sex
father.sex = "male";
因爲他們兩個的原型指向了同一個地方;
聖盃模式
Father.prototype.lastName = "tang"
function Father(){
}
function F(){
}
function Son() {
}
F.prototype = Father.prototype;
Son.prototype = new F();
function inherit (Target,Origin){
function F(){}
F.protptype = Origin.protptype;
Targrt.prototype = new F();
Targrt.prototype.constructor = Target;
Targrt.prototype.uber = Origin.prototype
}
Father.prototype.lastName = "wu";
function Father(){
}
function Son(){
};