JS中的phototype詳解
1 原型法設(shè)計模式
在.Net中可以使用clone()來實現(xiàn)原型法
原型法的主要思想是,現(xiàn)在有1個類A,我想要創(chuàng)建一個類B,這個類是以A為原型的,并且能進(jìn)行擴展。我們稱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,調(diào)用func方法
好了,下面一個一個問題解決:
prototype是什么含義?
javascript中的每個對象都有prototype屬性,Javascript中對象的prototype屬性的解釋是:返回對象類型原型的引用。
A.prototype = new B();
理解prototype不應(yīng)把它和繼承混淆。A的prototype為B的一個實例,可以理解A將B中的方法和屬性全部克隆了一遍。A能使用B的方法和屬性。這里強調(diào)的是克隆而不是繼承。可以出現(xiàn)這種情況: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的一個實例為原型克隆創(chuàng)建的。
那么就會有一個問題,如果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
實驗證明:函數(shù)運行時會先去本體的函數(shù)中去找,如果找到則運行,找不到則去prototype中尋找函數(shù)?;蛘呖梢岳斫鉃閜rototype不會克隆同名函數(shù)。
那么又會有一個新的問題:
如果我想使用extendClass的一個實例instance調(diào)用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當(dāng)做baseinstance來調(diào)用,調(diào)用它的對象方法showMsg”
好了,這里可能有人會問,為什么不用baseClass.showMsg.call(instance);
這就是對象方法和類方法的區(qū)別,我們想調(diào)用的是baseClass的對象方法
最后,下面這個代碼如果理解清晰,那么這篇文章說的就已經(jīng)理解了:
<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::showMsg
instance.baseShowMsg();//顯示baseClass::baseShowMsg
instance.showMsg();//顯示extendClass::showMsg
baseClass.showMsg.call(instance);//顯示baseClass::showMsg static
var baseinstance =new baseClass();
baseinstance.showMsg.call(instance);//顯示baseClass::showMsg
</script>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
webpack-url-loader 解決項目中圖片打包路徑問題
這篇文章主要介紹了webpack-url-loader 解決項目中圖片打包路徑問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
javaScript實現(xiàn)鼠標(biāo)在文字上懸浮時彈出懸浮層效果
這篇文章主要為大家詳細(xì)介紹了javaScript實現(xiàn)鼠標(biāo)在文字上懸浮時彈出懸浮層效果的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03

