JavaScript原型繼承_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
在傳統(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)建的對(duì)象的原型是:
new PrimaryStudent() ----> PrimaryStudent.prototype ----> Object.prototype ----> null
必須想辦法把原型鏈修改為:
new PrimaryStudent() ----> PrimaryStudent.prototype ----> Student.prototype ----> Object.prototype ----> null
這樣,原型鏈對(duì)了,繼承關(guān)系就對(duì)了。新的基于PrimaryStudent創(chuàng)建的對(duì)象不但能調(diào)用PrimaryStudent.prototype定義的方法,也可以調(diào)用Student.prototype定義的方法。
如果你想用最簡(jiǎn)單粗暴的方法這么干:
PrimaryStudent.prototype = Student.prototype;
是不行的!如果這樣的話,PrimaryStudent和Student共享一個(gè)原型對(duì)象,那還要定義PrimaryStudent干啥?
我們必須借助一個(gè)中間對(duì)象來實(shí)現(xiàn)正確的原型鏈,這個(gè)中間對(duì)象的原型要指向Student.prototype。為了實(shí)現(xiàn)這一點(diǎn),參考道爺(就是發(fā)明JSON的那個(gè)道格拉斯)的代碼,中間對(duì)象可以用一個(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對(duì)象,F(xiàn)對(duì)象的原型正好指向Student.prototype:
PrimaryStudent.prototype = new F();
// 把PrimaryStudent原型的構(gòu)造函數(shù)修復(fù)為PrimaryStudent:
PrimaryStudent.prototype.constructor = PrimaryStudent;
// 繼續(xù)在PrimaryStudent原型(就是new F()對(duì)象)上定義方法:
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
用一張圖來表示新的原型鏈:

注意,函數(shù)F僅用于橋接,我們僅創(chuàng)建了一個(gè)new F()實(shí)例,而且,沒有改變?cè)械腟tudent定義的原型鏈。
如果把繼承這個(gè)動(dòng)作用一個(gè)inherits()函數(shù)封裝起來,還可以隱藏F的定義,并簡(jiǎn)化代碼:
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;
};
小結(jié)
JavaScript的原型繼承實(shí)現(xiàn)方式就是:
1.定義新的構(gòu)造函數(shù),并在內(nèi)部用call()調(diào)用希望“繼承”的構(gòu)造函數(shù),并綁定this;
2.借助中間函數(shù)F實(shí)現(xiàn)原型鏈繼承,最好通過封裝的inherits函數(shù)完成;
3.繼續(xù)在新的構(gòu)造函數(shù)的原型上定義新方法。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
BootstrapValidator實(shí)現(xiàn)注冊(cè)校驗(yàn)和登錄錯(cuò)誤提示效果
這篇文章主要為大家詳細(xì)介紹了Bootstrap Validator實(shí)現(xiàn)注冊(cè)校驗(yàn)和登錄錯(cuò)誤提示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
本地對(duì)象Array的原型擴(kuò)展實(shí)現(xiàn)代碼
該擴(kuò)展方法目的為刪除數(shù)組中的重復(fù)項(xiàng),并返回一個(gè)包含所有重復(fù)條目的新數(shù)組;2010-12-12
element?table?點(diǎn)擊某一行中按鈕加載功能實(shí)現(xiàn)
在Element UI中,實(shí)現(xiàn)表格(element-table)中的這種功能通常涉及到數(shù)據(jù)處理和狀態(tài)管理,這篇文章主要介紹了element?table?點(diǎn)擊某一行中按鈕加載功能,需要的朋友可以參考下2024-06-06
微信小程序如何同時(shí)獲取用戶信息和用戶手機(jī)號(hào)
小程序登錄是現(xiàn)在小程序里面很普遍的一個(gè)功能,因?yàn)楣俜教峁┑姆椒?可以一鍵獲取到用戶信息,一鍵拿到手機(jī)號(hào),這篇文章主要給大家介紹了關(guān)于微信小程序如何同時(shí)獲取用戶信息和用戶手機(jī)號(hào)的相關(guān)資料,需要的朋友可以參考下2021-08-08

