在傳統(tǒng)的基于Class的語言如Java殉摔、C++中焰雕,繼承的本質(zhì)是擴(kuò)展一個(gè)已有的Class,并生成新的Subclass浊仆。
由于這類語言嚴(yán)格區(qū)分類和實(shí)例客峭,承實(shí)際上是類型的擴(kuò)展。但是抡柿,JavaScript由于采用原型繼承舔琅,我們無法直接擴(kuò)展一個(gè)Class,因?yàn)楦静淮嬖贑lass這種類型洲劣。
但是辦法還是有的备蚓。我們先回顧Student構(gòu)造函數(shù):
function Student(props) {
this.name = props.name || 'Unnamed';
}
Student.prototype.hello = function() {
alert('Hello, ' + this.name + '!');
}
以及Student的原型鏈:
現(xiàn)在课蔬,我們要基于Student擴(kuò)展出PrimaryStudent,可以先定義出PrimaryStudent:
function PrimaryStudent(props) {
// 調(diào)用Student構(gòu)造函數(shù)郊尝,綁定this變量:
Student.call(this, props);
this.grade = props.grade || 1;
}
但是二跋,調(diào)用了Student構(gòu)造函數(shù)不等于繼承了Student,PrimaryStudent創(chuàng)建的對象的原型是:
new PrimaryStudent() ----> PrimaryStudent.prototype ----> Object.prototype ----> null
必須想辦法把原型鏈修改為:
new PrimaryStudent() ----> PrimaryStudent.prototype ----> Student.prototype ----> Object.prototype ----> null
這樣流昏,原型鏈對了扎即,繼承關(guān)系就對了。新的基于PrimaryStudent創(chuàng)建的對象不但能調(diào)用PrimaryStudent.prototype定義的方法况凉,也可以調(diào)用Student.prototype定義的方法谚鄙。
如果你想用最簡單粗暴的方法這么干:
PrimaryStudent.prototype = Student.prototype;
是不行的!如果這樣的話刁绒,PrimaryStudent和Student共享一個(gè)原型對象闷营,那還要定義PrimaryStudent干啥?
我們必須借助一個(gè)中間對象來實(shí)現(xiàn)正確的原型鏈知市,這個(gè)中間對象的原型要指向Student.prototype傻盟。
為了實(shí)現(xiàn)這一點(diǎn),參考道爺(就是發(fā)明JSON的那個(gè)道格拉斯)的代碼嫂丙,中間對象可以用一個(gè)空函數(shù)F來實(shí)現(xiàn):
// PrimaryStudent構(gòu)造函數(shù):
function PrimaryStudent(props) {
Student.call(this, props);
this.grade = props.grade || 1;
}
// 空函數(shù)F:
function F() {
}
//把F的原型指向Student.prototype:
F.prototype = Student.prototype;
// 把PrimaryStudent的原型指向一個(gè)新的F對象莫杈,F(xiàn)對象的原型正好指向Student.prototype:
PrimaryStudent.prototype = new F();
// 把PrimaryStudent原型的構(gòu)造函數(shù)修復(fù)為PrimaryStudent:
PrimaryStudent.prototype.constructor = PrimaryStudent;
// 繼續(xù)在PrimaryStudent原型(就是new F()對象)上定義方法:
PrimaryStudent.prototype.getGrade = function () {
return this.grade;
};
// 創(chuàng)建xiaoming:
var xiaoming = new PrimaryStudent({
name: '小明',
grade: 2
});
xiaoming.name; // '小明'
xiaoming.grade; // 2
// 驗(yàn)證原型:
xiaoming.__proto__ === PrimaryStudent.prototype; // true
xiaoming.__proto__.__proto__ === Student.prototype; // true
// 驗(yàn)證繼承關(guān)系:
xiaoming instanceof PrimaryStudent; // true
xiaoming instanceof Student; // true
用一張圖來表示新的原型鏈:
<u>注意,函數(shù)F僅用于橋接奢入,我們僅創(chuàng)建了一個(gè)new F()實(shí)例筝闹,而且,沒有改變原有的Student定義的原型鏈腥光。</u>
如果把繼承這個(gè)動作用一個(gè)inherits()函數(shù)封裝起來关顷,還可以隱藏F的定義,并簡化代碼:
function inherits(Child, Parent) {
var F = function () {};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
}
這個(gè)inherits()函數(shù)可以復(fù)用:
function Student(props) {
this.name = props.name || 'Unnamed';
}
Student.prototype.hello = function () {
alert('Hello, ' + this.name + '!');
}
function PrimaryStudent(props) {
Student.call(this.props);
this.grade = props.grade || 1;
}
// 實(shí)現(xiàn)原型繼承鏈:
inherits(PrimaryStudent, Student);
// 綁定其他方法到PrimaryStudent原型:
PrimaryStudent.prototype.getGrade = function () {
return this.grade;
};
</br>
小結(jié)
JavaScript的原型繼承實(shí)現(xiàn)方式就是:
- 定義新的構(gòu)造函數(shù)武福,并在內(nèi)部用call()調(diào)用希望“繼承”的構(gòu)造函數(shù)议双,并綁定this;
- 借助中間函數(shù)F實(shí)現(xiàn)原型鏈繼承,最好通過封裝的inherits函數(shù)完成捉片;
- 繼續(xù)在新的構(gòu)造函數(shù)的原型上定義新方法平痰。