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

首頁 > 編程 > JavaScript > 正文

JS中的phototype詳解

2019-11-19 17:44:48
字體:
來源:轉載
供稿:網友

1 原型法設計模式

在.Net中可以使用clone()來實現原型法

原型法的主要思想是,現在有1個類A,我想要創建一個類B,這個類是以A為原型的,并且能進行擴展。我們稱B的原型為A。

2 javascript的方法可以分為三類:

a 類方法

b 對象方法

c 原型方法

例子:

functionPeople(name){this.name=name;//對象方法this.Introduce=function(){alert("My name is "+this.name);}}//類方法People.Run=function(){alert("I can run");}//原型方法People.prototype.IntroduceChinese=function(){alert("我的名字是"+this.name);}//測試var p1=newPeople("Windking"); p1.Introduce(); People.Run();p1.IntroduceChinese();

3 obj1.func.call(obj)方法

意思是將obj看成obj1,調用func方法

好了,下面一個一個問題解決:

prototype是什么含義?

javascript中的每個對象都有prototype屬性,Javascript中對象的prototype屬性的解釋是:返回對象類型原型的引用。

A.prototype = new B();

理解prototype不應把它和繼承混淆。A的prototype為B的一個實例,可以理解A將B中的方法和屬性全部克隆了一遍。A能使用B的方法和屬性。這里強調的是克隆而不是繼承。可以出現這種情況:A的prototype是B的實例,同時B的prototype也是A的實例。

先看一個實驗的例子:

function baseClass(){this.showMsg =function(){alert("baseClass::showMsg");}}function extendClass(){} extendClass.prototype =new baseClass();var instance =new extendClass();instance.showMsg();// 顯示baseClass::showMsg

我們首先定義了baseClass類,然后我們要定義extentClass,但是我們打算以baseClass的一個實例為原型,來克隆的extendClass也同時包含showMsg這個對象方法。

extendClass.prototype = new baseClass()就可以閱讀為:extendClass是以baseClass的一個實例為原型克隆創建的。

那么就會有一個問題,如果extendClass中本身包含有一個與baseClass的方法同名的方法會怎么樣?

下面是擴展實驗2:

function baseClass(){this.showMsg =function(){alert("baseClass::showMsg");}}function extendClass(){this.showMsg =function(){alert("extendClass::showMsg");}}extendClass.prototype =new baseClass();var instance =new extendClass();instance.showMsg();//顯示extendClass::showMsg

實驗證明:函數運行時會先去本體的函數中去找,如果找到則運行,找不到則去prototype中尋找函數。或者可以理解為prototype不會克隆同名函數。

那么又會有一個新的問題:

如果我想使用extendClass的一個實例instance調用baseClass的對象方法showMsg怎么辦?

答案是可以使用call:

extendClass.prototype =new baseClass();var instance =new extendClass();var baseinstance =new baseClass();baseinstance.showMsg.call(instance);//顯示baseClass::showMsg

這里的baseinstance.showMsg.call(instance);閱讀為“將instance當做baseinstance來調用,調用它的對象方法showMsg”

好了,這里可能有人會問,為什么不用baseClass.showMsg.call(instance);

這就是對象方法和類方法的區別,我們想調用的是baseClass的對象方法

最后,下面這個代碼如果理解清晰,那么這篇文章說的就已經理解了:

<script type="text/javascript">function baseClass(){this.showMsg =function(){alert("baseClass::showMsg");}this.baseShowMsg =function(){alert("baseClass::baseShowMsg");}}baseClass.showMsg =function(){alert("baseClass::showMsg static");}function extendClass(){this.showMsg =function(){alert("extendClass::showMsg");}}extendClass.showMsg =function(){alert("extendClass::showMsg static")}extendClass.prototype =new baseClass();var instance =new extendClass();instance.showMsg();//顯示extendClass::showMsginstance.baseShowMsg();//顯示baseClass::baseShowMsginstance.showMsg();//顯示extendClass::showMsgbaseClass.showMsg.call(instance);//顯示baseClass::showMsg staticvar baseinstance =new baseClass();baseinstance.showMsg.call(instance);//顯示baseClass::showMsg</script>

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 会同县| 贵德县| 建瓯市| 枣阳市| 文水县| 岱山县| 崇信县| 盐池县| 兰西县| 子长县| 子洲县| 阜新| 林芝县| 许昌县| 潜山县| 安泽县| 天津市| 饶阳县| 仪征市| 水富县| 稷山县| 丰台区| 张家口市| 博野县| 如东县| 乌拉特前旗| 时尚| 阿拉尔市| 高邑县| 龙里县| 东阳市| 施甸县| 抚州市| 承德市| 东海县| 彭泽县| 神农架林区| 平陆县| 柯坪县| 福泉市| 四平市|