這篇文章給大家分享的是有關(guān)js中繼承的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
我們是從2013年成立的成都網(wǎng)站建設(shè)公司,提供網(wǎng)站建設(shè),電商網(wǎng)站設(shè)計開發(fā),外貿(mào)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)頁設(shè)計,小程序開發(fā)、等服務(wù)。為客戶創(chuàng)造有價值的品牌營銷體驗(yàn),讓互聯(lián)網(wǎng)提升企業(yè)的競爭力!
繼承的簡介
繼承”是JavaScript面向?qū)ο笤O(shè)計的重要一環(huán),愿你認(rèn)真讀完本文,吃透繼承的概念。
繼承的核心

1. 繼承方式一:原型鏈
1.1 介紹
原型鏈?zhǔn)菍?shí)現(xiàn)繼承最原始的模式,即通過prototype屬性實(shí)現(xiàn)繼承。
//父級-構(gòu)造函數(shù)
function Father() {
this.fatherProp = true
}
//父級-原型屬性
Father.prototype.getFatherValue = function() {
return this.fatherProp
}
//子級-構(gòu)造函數(shù)
function Son() {
this.sonProp = false
}
//子級-原型屬性:繼承父級
//即__proto__指向父級的prototype
//若不理解請閱讀《一張圖徹底KO原型鏈(prototype,__proto__》
Son.prototype = new Father()
//子級-添加原型方法
Son.prototype.getSonValue = function() {
return this.sonProp
}
//創(chuàng)建子級的實(shí)例對象
var son = new Son()
console.log(son.getFatherValue()) //true1.2 解析:son實(shí)例對象是如何找到getFatherValue()方法的呢?
首先在son對象自身中找。若對象自身沒找到
然后在Son.prototype中找。若Son.prototype中沒找到
繼續(xù)往上一層,Son.prototype.__proto__(Fater.prototype)
依次類推,直到找到需要的屬性或方法,或到達(dá)原型鏈頂端Object.prototype
如果到最后都沒找到,會發(fā)生什么呢?
//一個不存在的方法 console.log(son.getValue()) //ERROE:not a function
1.3 注意事項(xiàng)
重寫父級原型鏈的方法或者添加父級原型鏈不存在的方法,必須在父級原型鏈代碼之后。(這個很好理解,不放代碼演示了)
通過原型鏈實(shí)現(xiàn)繼承后,不能再使用字面量的方式創(chuàng)建原型對象,因?yàn)闀采w原型鏈。
//子級-原型屬性:繼承父級
Son.prototype = new Father()
//不能像下面這樣,這樣會使得上面的代碼無效
//因?yàn)檫@相當(dāng)于重新創(chuàng)建了一個原型對象
Son.prototype = {
getSonValue: function() {
return this.sonProp
}
}1.4 原型鏈實(shí)現(xiàn)繼承的弊端
世間萬事萬物都不可能十全而十美,原型鏈雖然強(qiáng)大,但也存在缺陷。
原型鏈中引用類型的屬性會被所有實(shí)例共享的,即所有實(shí)例對象使用的是同一份數(shù)據(jù),會相互影響。
function Father() {
this.arr = [1,2,3]
}
function Son() {
}
Son.prototype = new Father()
var son1 = new Son()
console.log(son1.arr) //1,2,3
var son2 = new Son()
son2.arr.push(4)
console.log(son2.arr) //1,2,3,4
console.log(son1.arr) //1,2,3,4無法向父級構(gòu)造函數(shù)傳參
2. 繼承方式二:借用構(gòu)造函數(shù)
2.1 介紹
方式一中引用類型帶來的問題可借用構(gòu)造函數(shù)的方式解決。其核心思想是:在子級構(gòu)造函數(shù)中調(diào)用父級構(gòu)造函數(shù)。
如何實(shí)現(xiàn)在一個構(gòu)造函數(shù)中調(diào)用另一個函數(shù)?——call()和apply()
function Father() {
this.arr = [1,2,3]
}
function Son() {
//call的第一個函數(shù)是this指向的對象,即構(gòu)造函數(shù)的實(shí)例對象
Father.call(this)
/*上面代碼等同于下面這段代碼:
(function() {
this.arr = [1,2,3]
}).call(this)
*/
}
var son1 = new Son()
console.log(son1.arr) //1,2,3
var son2 = new Son()
son2.arr.push(4)
console.log(son2.arr) //1,2,3,4
console.log(son1.arr) //1,2,3//解決傳參問題:
function Father(name) {
this.name = name
}
function Son(name) {
Father.call(this, name)
}
var son1 = new Son("小名")
console.log(son1.name) //小名
var son2 = new Son("一燈")
console.log(son2.name) //一燈2.2 借用構(gòu)造函數(shù)的缺陷
這種方式是通過構(gòu)造函數(shù)實(shí)現(xiàn)的,當(dāng)然也把構(gòu)造函數(shù)自身的問題帶過來了——破壞了復(fù)用性。因?yàn)槊總€實(shí)例都創(chuàng)建了一份副本。
3. 組合繼承
3.1 介紹
組合繼承 = 原型鏈 + 借用構(gòu)造函數(shù)。取其長避其短:共享的用原型鏈,各自的借用構(gòu)造函數(shù)
function Father(name) {
this.name = name
this.arr = [1,2,3]
}
Father.prototype.getName = function() {
console.log(this.name)
}
function Son(name, age) {
Father.call(this, name)
this.age = age
}
Son.prototype = new Father()
Son.prototype.constructor = Son
Son.prototype.getAge = function() {
console.log(this.age)
}
var son1 = new Son("小名", 23)
son1.arr.push(4)
console.log(son1.arr) //1,2,3,4
son1.getName() //小名
son1.getAge() //23
var son2 = new Son("一燈", 24)
console.log(son2.arr) //1,2,3
son1.getName() //一燈
son1.getAge() //24感謝各位的閱讀!關(guān)于“js中繼承的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
網(wǎng)頁題目:js中繼承的示例分析
URL鏈接:http://www.chinadenli.net/article16/gccjdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、服務(wù)器托管、云服務(wù)器、網(wǎng)站營銷、Google、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)