您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)js中繼承的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
繼承的簡介
繼承”是JavaScript面向?qū)ο笤O(shè)計(jì)的重要一環(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()) //true
1.2 解析:son實(shí)例對象是如何找到getFatherValue()方法的呢?
首先在son對象自身中找。若對象自身沒找到
然后在Son.prototype中找。若Son.prototype中沒找到
繼續(xù)往上一層,Son.prototype.__proto__(Fater.prototype)
依次類推,直到找到需要的屬性或方法,或到達(dá)原型鏈頂端Object.prototype
如果到最后都沒找到,會(huì)發(fā)生什么呢?
//一個(gè)不存在的方法 console.log(son.getValue()) //ERROE:not a function
1.3 注意事項(xiàng)
重寫父級原型鏈的方法或者添加父級原型鏈不存在的方法,必須在父級原型鏈代碼之后。(這個(gè)很好理解,不放代碼演示了)
通過原型鏈實(shí)現(xiàn)繼承后,不能再使用字面量的方式創(chuàng)建原型對象,因?yàn)闀?huì)覆蓋原型鏈。
//子級-原型屬性:繼承父級 Son.prototype = new Father() //不能像下面這樣,這樣會(huì)使得上面的代碼無效 //因?yàn)檫@相當(dāng)于重新創(chuàng)建了一個(gè)原型對象 Son.prototype = { getSonValue: function() { return this.sonProp } }
1.4 原型鏈實(shí)現(xiàn)繼承的弊端
世間萬事萬物都不可能十全而十美,原型鏈雖然強(qiáng)大,但也存在缺陷。
原型鏈中引用類型的屬性會(huì)被所有實(shí)例共享的,即所有實(shí)例對象使用的是同一份數(shù)據(jù),會(huì)相互影響。
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è)構(gòu)造函數(shù)中調(diào)用另一個(gè)函數(shù)?——call()和apply()
function Father() { this.arr = [1,2,3] } function Son() { //call的第一個(gè)函數(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)槊總€(gè)實(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é)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。