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

首頁 > 編程 > JavaScript > 正文

JavaScript模擬實現繼承的方法

2019-11-20 12:49:55
字體:
來源:轉載
供稿:網友

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

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

先定義一個父類,

//父類function ParentClass() { this.className = "ParentClass"; this.auth = "Auth"; this.version = "V1.0"; this.parentClassInfo = function () { return this.className + "/n" + this.auth + "/n" + this.version; }}

一、prototype 實現:

//子類//1、prototype繼承function ChildClassByPrototype() { this.date = "2013-07-26"; this.classInfo = function () {  return this.parentClassInfo() + "/n" + this.date; }}ChildClassByPrototype.prototype = new ParentClass();var cctest1 = new ChildClassByPrototype();cctest1.parentClassInfo();cctest1.classInfo();

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

二、apply 實現:

//2、apply繼承function ChildClassByApply() { ParentClass.apply(this, new Array()); //ParentClass.apply(this, []); this.date = "2013-07-26"; this.classInfo = function () {  return this.parentClassInfo() + "/n" + this.date; }}

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

三、call + prototype 實現:

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

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

每種方法都有其適用環境,比如,如果父類帶有有參構造函數:

function ParentClass(className, auth, version) { this.className = className; this.auth = auth; this.version = version; this.parentClassInfo = function () { return this.className + "/n" + this.auth + "/n" + this.version; }}

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

function ChildClassByApply(className, auth, version) { ParentClass.apply(this, [className, auth, version]); this.date = "2013-07-26"; this.classInfo = function () {  return this.parentClassInfo() + "/n" + this.date; }}function ChildClassByCall(className, auth, version) { ParentClass.call(this, arguments[0], arguments[1], arguments[2]); //ParentClass.call(this, className, auth, version); this.date = "2013-07-26"; this.classInfo = function () {  return this.parentClassInfo() + "/n" + this.date; }}ChildClassByCall.prototype = new ParentClass();

實例化:

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

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

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临城县| 松桃| 马尔康县| 安龙县| 竹北市| 虹口区| 河东区| 稷山县| 正宁县| 徐水县| 锡林郭勒盟| 焦作市| 汝城县| 上犹县| 清水河县| 泰州市| 沙湾县| 大丰市| 乌什县| 富宁县| 溧阳市| 丹寨县| 容城县| 临朐县| 江陵县| 万州区| 周至县| 抚顺市| 平顺县| 涿州市| 资阳市| 年辖:市辖区| 汉川市| 辽宁省| 巩义市| 宣化县| 靖州| 西乌| 西青区| 凌云县| 寻乌县|