最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript原型繼承_動力節(jié)點Java學院整理

 更新時間:2017年06月30日 14:47:05   作者:liaoxuefeng  
這篇文章主要為大家詳細介紹了JavaScript原型繼承的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在傳統(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;

是不行的!如果這樣的話,PrimaryStudentStudent共享一個原型對象,那還要定義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ù)修復為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ù)可以復用:
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)文章

最新評論

喀什市| 邵阳市| 海丰县| 稻城县| 太和县| 太和县| 文化| 乐都县| 靖州| 遂宁市| 咸丰县| 鱼台县| 汝州市| 竹北市| 阳信县| 盐源县| 山阴县| 治县。| 伊川县| 敖汉旗| 弋阳县| 梧州市| 沿河| 西藏| 民权县| 庄河市| 余庆县| 班玛县| 荆门市| 武冈市| 民乐县| 尉犁县| 上饶县| 绥宁县| 收藏| 宾川县| 仁化县| 尖扎县| 巢湖市| 修文县| 建平县|