天天看點

javascript Prototype constructor的了解(轉)

講js的構造的,這個比較清晰,但并不表示一定正确。

這幾天一直在思考這個東東,感覺比以前了解更深入了。

http://blog.csdn.net/chunqiuwei/article/details/22092551

javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)

url:

  ==========================================================================

=================================================================================

在學習js的面向對象過程中,一直對constructor與prototype感到很迷惑,看了一些部落格與書籍,覺得自己弄明白了,現在記錄如下:

     我們都知道,在js中有一個function的東西。一般人們叫它函數。比如下面的代碼

function person(name)   

{   

  alert(name);   

}   

person('js');//js  

上面的代碼中,person的表現的确跟一般的函數沒有什麼差別,接着看下面的代碼

javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)

代碼

   this.name=name;   

   this.showme=function()   

        {   

           alert(this.name);   

        }   

};   

var one=new person('javascript');   

one.showme();//javascript  

javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)

很多人見到了久違的new操作符,于是就叫person為“類”,可是又沒有關鍵字class的出現,覺得叫“類”有點勉強。于是退而求其次叫 person為類的構造函數。這些概念好像都沒有錯,之是以出現這樣的情況,可能是因為大家都學習了傳統的面向對象語言(c++,c#,java等),還 有一種思維定勢吧。為了讓javascript也面向對象,要在javascript中找到與傳統面向對象語言的影子。可是按照javascript的說 法,function定義的這個person就是一個object(對象),而且還是一個很特殊的對象,這個使用function定義的對象與使用new 操作符生成的對象之間有一個重要的差別。這個差別就是function定義的對象有一個prototype屬性,使用new生成的對象就沒有這個prototype屬性。

    prototype屬性又指向了一個prototype對象,注意prototype屬性與prototype對象是兩個不同的東西,要注意差別。在prototype對象中又有一個constructor屬性,這個constructor屬性同樣指向一個constructor對象,而這個constructor對象恰恰就是這個function函數本身。

有點頭暈,看下圖吧:

javascript Prototype constructor的了解(轉)

不相信可以看下面的代碼:

javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)

var one=new person('js');   

alert(one.prototype)//undefined   

alert(typeof person.prototype);//object   

alert(person.prototype.constructor);//function person(name) {...};  

javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)

上面的代碼證明了one這個對象沒有prototype屬性。

我們接着看代碼:

javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)

person.prototype.from=function()   

  alert('i come from prototype.');   

one.showme();//js,這個結果沒有什麼好奇怪的   

one.from();//i come from prototype.,這個結果有一點奇怪吧  

javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)

要解釋這個結果就要仔細研究一下new這個操作符了.var one=new person('js');這個語句執行的過程可以分成下面的語句:

var one={};   

person.call(one,'js');  

按照《悟透javascript》書中說的,new形式建立對象的過程實際上可以分為三步:

第一步是建立一個新對象(叫a吧);

第二步将該對象(a)内置的原型對象設定為構造函數(就是person)prototype 屬性引用的那個原型對象;

第三步就是将該對象(a)作為this 參數調用構造函數(就是person),完成成員設定等初始化工作。

其中第二步中出現了一個新名詞就是内置的原型對象,注意這個新名詞跟prototype對象不是一回事,為了差別我叫它inobj,inobj就指向了函數person的prototype對象。在person的prototype對象中出現的任何屬性或者函數都可以在one對象中直接使用,這個就是javascript中的原型繼承了。

又頭暈了,上圖吧!

javascript Prototype constructor的了解(轉)

這樣one對象通過内置的原型對象inobj就可以直接通路person的prototype對象中的任何屬性與方法了。這也就解釋了上面的代碼中 為什麼one可以通路form函數了。因為prototype對象中有一個constructor屬性,那麼one也可以直接通路constructor 屬性。

javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)

one.from();//i come from prototype.,這個結果有一點奇怪吧   

alert(one.constructor);//function person(name) {...}   

alert(person.prototype.constructor);//function person(name) {...}  

javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)

 接着看繼承是如何實作的。

javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)

function subperson()   

subperson.prototype=new person();   

var subone=new subperson();   

subone.from();//i come from prototype.   

alert(subone.constructor);//function person(name) {...};   

alert(subperson.prototype.constructor);//function person(name) {...};  

javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)

繼承的實作很簡單,隻需要把子類的prototype設定為父類的一個(執行個體化)對象即可。注意這裡說的可是對象哦!

 那麼通過prototype屬性實作繼承的原理是什麼呢?還是先看圖形說明,然後編寫代碼進行驗證。

javascript Prototype constructor的了解(轉)

注意:紅色的方框就是把子類與父類連結起來的地方。這個就應該是傳說中的prototype鍊了吧。下面有代碼進行驗證。

javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)

var father=new person('js');//為了下面示範使用showme方法,采用了js參數,實際多采用無參數   

alert(father.constructor);//檢視構造函數,結果是:function person(name) {...};   

function subper()   

subper.prototype=father;//注意這裡   

subper.prototype.constructor=subper;   

var son=new subper();   

son.showme();//js   

son.from();//i come from prototype.   

alert(father.constructor);//function subper(){...}   

alert(son.constructor);//function subper(){...}   

alert(subper.prototype.constructor);//function subper(){...}  

javascript Prototype constructor的了解(轉)
javascript Prototype constructor的了解(轉)

根據上圖的prototype鍊,還有代碼的結果,我想應該明白為什麼使用prototype能夠實作

js中的繼承了吧。