寫(xiě)在最前面
繼承的發(fā)展史
- 傳統(tǒng)形式 --> 原型鏈
- 借用構(gòu)造函數(shù)
- 不能繼承借用構(gòu)造函數(shù)的原型
- 每次構(gòu)造函數(shù)都要多走一個(gè)函數(shù)
- 共享原型
- 圣杯模式
原型鏈的繼承
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();
借用構(gòu)造函數(shù)繼承
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();
//缺點(diǎn)不能隨便修改自己的原型
比如我在下面
Son.prototype.sex = "male";
son.sex//打印出"male";
father.sex = "male";
因?yàn)樗麄儍蓚€(gè)的原型指向了同一個(gè)地方;
圣杯模式
Father.prototype.lastName = "tang"
function Father(){
}
//我自己設(shè)置個(gè)中間層
function F(){
}
function Son() {
}
F.prototype = Father.prototype;
Son.prototype = new F();
//設(shè)置成一個(gè)中間層
//這樣子寫(xiě)的好處就是son在自己的原型上面加?xùn)|西
// 不會(huì)影響別的原型,因?yàn)閟on的原型是new F(); new F的原型才是Father.prototype
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(){
};
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。