JavaScript中繼承原理與用法實例入門
本文實例講述了JavaScript中繼承原理與用法。分享給大家供大家參考,具體如下:
正統(tǒng)的面相對象的語言都會提供extend之類的方法用于出來類的繼承,但Javascript并不提供extend方法,在Javascript中使用繼承需要用點技巧。
Javascript中的實例的屬性和行為是由構(gòu)造函數(shù)和原型兩部分組成的,我們定義兩個類:Person和zhangsan,它們在內(nèi)存中的表現(xiàn)如下圖1:如果想讓Zhangsan繼承Person,那么我們需要把Person構(gòu)造函數(shù)和原型中的屬性和行為全部傳給Zhangsan的構(gòu)造函數(shù)和原型,如下圖2所示:
Are you Ok?了解了繼承的思路后,那么我們一步步完成Person和Zhangsan的繼承功能。首先,我們需要定義Person類,如下代碼:[代碼1]
// 定義Person類function Person (name){ this.name = name; this.type = '人';}Person.prototype={ say : function(){ console.info('我是一個'+ this.type +',我的名字叫' + this.name); }} //定義Zhangsan類function Zhangsan (name){}Zhangsan.prototype={ }
Zhangsan雖然有自己特有的屬性和行為,但它大部分屬性和行為和Person相同,需要繼承自Person類。如前所述,JavaScript中繼承是要分別繼承構(gòu)造函數(shù)和原型中的屬性和行為的。我們先讓Zhangsan繼承Person的構(gòu)造函數(shù)中的行為和屬性,如下代碼:[代碼2]
// 定義Person類function Person (name){ this.name = name; this.type = '黃';}Person.prototype={ say : function(){ console.info('我是一個'+ this.type +'種人,我的名字叫' + this.name); }} //定義Zhangsan類function Zhangsan (name){ this.name = name; this.type = '黃';}Zhangsan.prototype={}//實例化Zhangsan對象var zs = new Zhangsan('張三');console.info(zs.type); // 黃
運行正常,但我們怎么沒看到繼承的“味道”呢?我們在Zhangsan的構(gòu)造函數(shù)中將Person的屬性和行為復(fù)制了一份,與其說是繼承不如說是“真巧,這兩個類的構(gòu)造函數(shù)除了函數(shù)名不同,其他地方都長得一樣”。她的缺點很明顯:如果Person類的構(gòu)造函數(shù)有任何變動,我們也需要手動的同步修改Zhangsan類的構(gòu)造函數(shù),同樣一份代碼,我們復(fù)制了一份寫在了程序中 的不同地方,這違法了DRY原則,降低了代碼的可維護性。
好了,讓我們來改進它:[代碼3]
// 定義Person類function Person (name){ this.name = name; this.type = '黃';}Person.prototype={ say : function(){ console.info('我是一個'+ this.type +'種人,我的名字叫' + this.name); }} // 定義Zhangsan類function Zhangsan (name){ Person(name);}Zhangsan.prototype={}// 實例化Zhangsan對象var zs = new Zhangsan('張三');console.info(zs.type); // undefined
我們在Zhangsan的構(gòu)造函數(shù)里調(diào)用Person()函數(shù),希望它內(nèi)部的ths.xxx可以在Zhangsan類的構(gòu)造函數(shù)里執(zhí)行一遍,但奇怪的是,出現(xiàn)“console.info(zs.type);”時,輸出的是undefined,這是怎么回事呢?
這和Person的調(diào)用方式有關(guān)。在JavaScript中,function有兩種不同的調(diào)用方法:
作為函數(shù)存在,直接用“()”調(diào)用,例如“function test(){}; test();”test被用作函數(shù),直接被“()”符號調(diào)用。
作為類的構(gòu)造函數(shù)存在,使用new調(diào)用,例如“function test(){}; new test();”test作為類的構(gòu)造函數(shù),通過new進行test類的實例化。這兩種方法的調(diào)用,function內(nèi)部的this指向會有所不同---作為函數(shù)的function,其this指向的是window,而作為構(gòu)造函數(shù)的function,其this指向的實例對象。
上面代碼中,Zhangsan類構(gòu)造函數(shù)中的Person是通過函數(shù)方式調(diào)用的,它內(nèi)部的this指向的是window對象,起效果等同于如下代碼:[代碼4]
// 定義Person類function Person (name){ this.name = name; this.type = '黃';}Person.prototype={ say : function(){ console.info('我是一個'+ this.type +'種人,我的名字叫' + this.name); }} // 定義Zhangsan類function Zhangsan (name){ window.name = name; window.type = '黃';}Zhangsan.prototype={}// 實例化Zhangsan對象var zs = new Zhangsan('張三');console.info(zs.type); // undefinedconsole.info(type); // 黃 (window.type可以省略寫成type)
如果想達到[代碼3]的效果,讓Person內(nèi)部this指向Zhangsan類的實例,可以通過call或apply方法實現(xiàn),如下:[代碼5]
// 定義Person類function Person (name){ this.name = name; this.type = '黃';}Person.prototype={ say : function(){ console.info('我是一個'+ this.type +'種人,我的名字叫' + this.name); }} // 定義Zhangsan類function Zhangsan (name){ Person.call(this,name);}Zhangsan.prototype={}// 實例化Zhangsan對象var zs = new Zhangsan('張三');console.info(zs.type); // 黃
構(gòu)造函數(shù)的屬性和行為已經(jīng)成功實現(xiàn)了繼承,接下來我們要實現(xiàn)原型中的屬性和行為的繼承。既然Zhangsan類需要和Person類原型中同樣的屬性和行為,那么能否將Person類的原型直接傳給Zhangsan類的原型,如下代碼:[代碼6]
// 定義Person類function Person (name){ this.name = name; this.type = '黃';}Person.prototype={ say : function(){ console.info('我是一個'+ this.type +'種人,我的名字叫' + this.name); }} // 定義Zhangsan類function Zhangsan (name){ Person.call(this,name);}Zhangsan.prototype = Person.prototype;// 實例化Zhangsan對象var zs = new Zhangsan('張三');// 我是一個黃種人,我的名字叫張三zs.say();
通過Person類的原型傳給Zhangsan類的原型,Zhangsan類成功獲得了say行為,但事情并不像想象中的那么簡單,如果我們要給Zhangsan類添加run行為呢?如下代碼:[代碼7:添加run行為]
// 定義Person類function Person (name){ this.name = name; this.type = '黃';}Person.prototype={ say : function(){ console.info('我是一個'+ this.type +'種人,我的名字叫' + this.name); }} // 定義Zhangsan類function Zhangsan (name){ Person.call(this,name);}Zhangsan.prototype = Person.prototype;Zhangsan.prototype.run = function(){ console.info('我100米短跑只要10秒!');}// 實例化Zhangsan對象var zs = new Zhangsan('張三');zs.say(); // 我是一個黃種人,我的名字叫張三zs.run(); //我100米短跑只要10秒!var zs2 = new Person('張三2');zs2.run(); //我100米短跑只要10秒!
我們只想給Zhangsan類添加run行為,為什么Person類也獲得了run行為了呢?這涉及傳值和傳址的兩個問題----在JavaScript中,賦值語句會用傳值和傳地址兩種不同的方式進行賦值,如果是數(shù)值型、不爾型、字符型等基本數(shù)據(jù)類型,在進行賦值時會將數(shù)據(jù)直接賦值一份,將賦值的那一份數(shù)據(jù)進行賦值,也就是通常所說的傳值;如果是數(shù)組、hash對象等復(fù)雜數(shù)據(jù)類型,在進行賦值時會直接用內(nèi)存地址賦值,而不是將數(shù)據(jù)賦值一份,這就是傳址賦值,就是傳數(shù)據(jù)的映射地址。[代碼8:傳值與傳址]
var a=10; // 基本數(shù)據(jù)類型var b=a; // 將變量a保存的值賦值一份,傳給變量b,b和a各保存一份數(shù)據(jù)var c=[1,2,3]; // 復(fù)雜數(shù)據(jù)類型var d=c; // 將變量c指向的數(shù)據(jù)內(nèi)存地址傳給變量d,c和d指向同一份數(shù)據(jù)b++;d.push(4);console.info(a); // 10console.info(b); // 11 變量b保存的數(shù)據(jù)更改不會影響到變量aconsole.info(c); // 1,2,3,4 變量c和d指向同一份數(shù)據(jù),數(shù)據(jù)更改會相互影響console.info(d); // 1,2,3,4
在原生JavaScript中,選擇傳值還是傳地址是根據(jù)數(shù)據(jù)類型來自動判斷的,但傳地址有時候會給我們帶來意想不到的麻煩,所以我們需要對復(fù)雜數(shù)據(jù)類型的賦值進行控制,讓復(fù)雜數(shù)據(jù)類型也可以進行傳值。
最簡單的做法是遍歷數(shù)組或者Hash對象,將數(shù)組或者Hash對象這種復(fù)雜的數(shù)據(jù)拆分成一個個簡單數(shù)據(jù),然后分別賦值,如下面代碼:[代碼9:對復(fù)雜數(shù)據(jù)類型進行傳值]
var a = [1, 2, 3] ,b = {name:’張三’,sex:’男’,tel:’1383838438’};var c = [] ,d = {};for(var p in a){ c[p] = a[p]; }for(var p in b){ d[p] = b[p];}c.push(’4’);d.email = ’ibing@outlook.com’;console.info(a); // [1, 2, 3]console.info(c); // [1, 2, 3, '4']console.info(b.email); // undefinedconsole.info(d.email); // ibing@outlook.com
值得一提的是,對于數(shù)組的傳值還可以使用數(shù)組類的slice或者concat方法實現(xiàn),如下面代碼:[代碼10:數(shù)組傳值的簡單方法]
var a = [1, 2, 3];var b = a.slice(), c = a.concat();b.pop();c.push(4);console.info(a); // [1, 2, 3]console.info(b); // [1, 2]console.info(c); // [1, 2, 3, 4]
prototype本質(zhì)上也是一個hash對象,所以直接用它賦值時會進行傳址,這也是為什么[代碼7:添加潤行為]中,zs2居然會run的原因。我們可以用for in來遍歷prototype,從而實現(xiàn)prototype的傳值。但因為prototype和function(用做類的function)的關(guān)系,我們還有另外一種方法實現(xiàn)prototype的傳值----new SomeFunction(),如下面代碼:[代碼11]
// 定義Person類function Person (name){ this.name = name; this.type = '黃';}Person.prototype={ say : function(){ console.info('我是一個'+ this.type +'種人,我的名字叫' + this.name); }} // 定義Zhangsan類function Zhangsan (name){ Person.call(this,name);}Zhangsan.prototype = new Person();Zhangsan.prototype.constructor = Person;Zhangsan.prototype.run = function(){ console.info('我100米短跑只要10秒!');} // 實例化Zhangsan對象var zs = new Zhangsan('張三');zs.say(); // 我是一個黃種人,我的名字叫張三zs.run(); // 我100米短跑只要10秒!var zs2 = new Person('張三2');zs2.run(); // TypeError: zs2.run is not a function
您是否注意到上面這句Zhangsan.prototype.constructor = Person;,這是因為Zhangsan.prototype = new Person();時,Zhangsan.prototype.constructor指向了Person,我們需要將它糾正,重新指向Zhangsan。
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章:
