国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JavaScript模擬實現(xiàn)繼承的方法

2024-05-06 16:17:50
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了JavaScript模擬實現(xiàn)繼承的方法,實例分析了javascript類的操作與模擬實現(xiàn)繼承的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了JavaScript模擬實現(xiàn)繼承的方法。分享給大家供大家參考。具體分析如下:

我們都知道,在JavaScript中只能模擬實現(xiàn)OO中的"類",也就意味著,在JavaScript中沒有類的繼承。我們也只能通過在原對象里添加或改寫屬性來模擬實現(xiàn)。

先定義一個父類,

 

 
  1. //父類 
  2. function ParentClass() { 
  3. this.className = "ParentClass"
  4. this.auth = "Auth"
  5. this.version = "V1.0"
  6. this.parentClassInfo = function () { 
  7. return this.className + "/n" + this.auth + "/n" + this.version; 

一、prototype 實現(xiàn):

 

 
  1. //子類 
  2. //1、prototype繼承 
  3. function ChildClassByPrototype() { 
  4. this.date = "2013-07-26"
  5. this.classInfo = function () { 
  6. return this.parentClassInfo() + "/n" + this.date; 
  7. ChildClassByPrototype.prototype = new ParentClass(); 
  8. var cctest1 = new ChildClassByPrototype(); 
  9. cctest1.parentClassInfo(); 
  10. cctest1.classInfo(); 

這種方式很簡單,只需把父類的實例賦值給子類的prototype屬性就行了,然后子類就可以使用父親的方法和屬性了。這里其實是用到了原型鏈向上查找的特性,比如這個例子中的 cctest1.parentClassInfo() 方法,JavaScript會先在ChildClassByPrototype的實例中查找是否有parentClassInfo()方法,子類中沒有,所以繼續(xù)查找ChildClassByPrototype.prototype屬性,而其prototype屬性的值是ParentClass的一個實例,該實例有parentClassInfo()方法,于是查找結束,調用成功。

二、apply 實現(xiàn):

 

 
  1. //2、apply繼承 
  2. function ChildClassByApply() { 
  3. ParentClass.apply(thisnew Array()); 
  4. //ParentClass.apply(this, []); 
  5. this.date = "2013-07-26"
  6. this.classInfo = function () { 
  7. return this.parentClassInfo() + "/n" + this.date; 

JavaScript中的apply可以理解為用A方法替換B方法,第一個參數(shù)為B方法的對象本身,第二個參數(shù)為一個數(shù)組,該數(shù)組內的值為需要傳遞給A方法對應的參數(shù)列表,如果參數(shù)為空,即沒有參數(shù)傳遞,可通過 new Array()、[] 來傳遞。

三、call + prototype 實現(xiàn):

 

 
  1. //3、call+prototype繼承 
  2. function ChildClassByCall() { 
  3. ParentClass.call(this, arguments); 
  4. this.date = "2013-07-26"
  5. this.classInfo = function () { 
  6. return this.parentClassInfo() + "/n" + this.date; 
  7. ChildClassByCall.prototype = new ParentClass(); 

call和apply作用類似,即都是用A方法替換B方法,只是傳遞的參數(shù)不一樣,call方法的第一個參數(shù)為B方法的對象本身,后續(xù)的參數(shù)則不用Array包裝,需直接依次進行傳遞。既然作用差不多,為何多了一句 原型賦值呢?這是因為call方法只實現(xiàn)了方法的替換而沒有對對象屬性進行復制操作。

每種方法都有其適用環(huán)境,比如,如果父類帶有有參構造函數(shù):

 

 
  1. function ParentClass(className, auth, version) { 
  2. this.className = className; 
  3. this.auth = auth; 
  4. this.version = version; 
  5. this.parentClassInfo = function () { 
  6. return this.className + "/n" + this.auth + "/n" + this.version; 

這種情況下,prototype就不適用了,可選用apply或call;

 

 
  1. function ChildClassByApply(className, auth, version) { 
  2. ParentClass.apply(this, [className, auth, version]); 
  3. this.date = "2013-07-26"
  4. this.classInfo = function () { 
  5. return this.parentClassInfo() + "/n" + this.date; 
  6. function ChildClassByCall(className, auth, version) { 
  7. ParentClass.call(this, arguments[0], arguments[1], arguments[2]); 
  8. //ParentClass.call(this, className, auth, version); 
  9. this.date = "2013-07-26"
  10. this.classInfo = function () { 
  11. return this.parentClassInfo() + "/n" + this.date; 
  12. ChildClassByCall.prototype = new ParentClass(); 

實例化:

 

 
  1. var cctest2 = new ChildClassByApply("ParentClass""Auth""V1.0"); 
  2. var cctest3 = new ChildClassByCall("ParentClass""Auth""V1.0"); 

在apply和call中,又該如何取舍呢?在OO的繼承中,子類繼承于父類,那么它應該也是父類的類型。即,ChildClassByCall、ChildClassByApply應該也是ParentClass類型,但我們用"instanceof"檢測一下就會發(fā)現(xiàn),通過apply繼承的子類,并非ParentClass類型。所以,我們建議用call + prototype 來模擬實現(xiàn)繼承。據(jù)說,Google Map API 的繼承就是使用這種方式喲。

希望本文所述對大家的javascript程序設計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 松桃| 大连市| 佳木斯市| 怀集县| 介休市| 左云县| 宝兴县| 兴山县| 合阳县| 高邑县| 扎鲁特旗| 怀集县| 兖州市| 屏山县| 六枝特区| 策勒县| 汶川县| 同江市| 康平县| 米易县| 安福县| 高尔夫| 腾冲县| 大连市| 广州市| 本溪| 抚顺市| 望奎县| 平陆县| 兴隆县| 赣榆县| 吉木萨尔县| 昌邑市| 竹山县| 安徽省| 拜城县| 潜江市| 鄂托克前旗| 红安县| 达日县| 库伦旗|