JavaScript原型繼承_動力節(jié)點Java學院整理
在傳統(tǒng)的基于Class的語言如Java、C++中,繼承的本質(zhì)是擴展一個已有的Class,并生成新的Subclass。
由于這類語言嚴格區(qū)分類和實例,繼承實際上是類型的擴展。但是,JavaScript由于采用原型繼承,我們無法直接擴展一個Class,因為根本不存在Class這種類型。
但是辦法還是有的。我們先回顧Student構(gòu)造函數(shù):
function Student(props) { this.name = props.name || 'Unnamed'; } Student.prototype.hello = function () { alert('Hello, ' + this.name + '!'); }
以及Student的原型鏈:
現(xiàn)在,我們要基于Student擴展出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共享一個原型對象,那還要定義PrimaryStudent干啥?
我們必須借助一個中間對象來實現(xiàn)正確的原型鏈,這個中間對象的原型要指向Student.prototype。為了實現(xiàn)這一點,參考道爺(就是發(fā)明JSON的那個道格拉斯)的代碼,中間對象可以用一個空函數(shù)F來實現(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的原型指向一個新的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 // 驗證原型: xiaoming.__proto__ === PrimaryStudent.prototype; // true xiaoming.__proto__.__proto__ === Student.prototype; // true // 驗證繼承關(guān)系: xiaoming instanceof PrimaryStudent; // true xiaoming instanceof Student; // true
用一張圖來表示新的原型鏈:
注意,函數(shù)F僅用于橋接,我們僅創(chuàng)建了一個new F()實例,而且,沒有改變原有的Student定義的原型鏈。
如果把繼承這個動作用一個inherits()函數(shù)封裝起來,還可以隱藏F的定義,并簡化代碼:
function inherits(Child, Parent) { var F = function () {}; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; } 這個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; } // 實現(xiàn)原型繼承鏈: inherits(PrimaryStudent, Student); // 綁定其他方法到PrimaryStudent原型: PrimaryStudent.prototype.getGrade = function () { return this.grade; };
小結(jié)
JavaScript的原型繼承實現(xiàn)方式就是:
1.定義新的構(gòu)造函數(shù),并在內(nèi)部用call()調(diào)用希望“繼承”的構(gòu)造函數(shù),并綁定this;
2.借助中間函數(shù)F實現(xiàn)原型鏈繼承,最好通過封裝的inherits函數(shù)完成;
3.繼續(xù)在新的構(gòu)造函數(shù)的原型上定義新方法。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
BootstrapValidator實現(xiàn)注冊校驗和登錄錯誤提示效果
這篇文章主要為大家詳細介紹了Bootstrap Validator實現(xiàn)注冊校驗和登錄錯誤提示效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03element?table?點擊某一行中按鈕加載功能實現(xiàn)
在Element UI中,實現(xiàn)表格(element-table)中的這種功能通常涉及到數(shù)據(jù)處理和狀態(tài)管理,這篇文章主要介紹了element?table?點擊某一行中按鈕加載功能,需要的朋友可以參考下2024-06-06