最初對js中 object.constructor 的認識:

來源:互聯網
上載者:User

標籤:


 

 

 

 

 

 

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

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

 

function Person(name)   
{   
  alert(name);   
}   
Person(‘js‘);//js  

 


 

上面的代碼中,Person的表現的確跟一般的函數沒有什麼區別,接著看下面的代碼

 

代碼function Person(name)   
{   
   this.name=name;   
   this.showMe=function()   
        {   
           alert(this.name);   
        }   
};   
var one=new Person(‘JavaScript‘);   
one.showMe();//JavaScript  

 


 

很多人見到了久違的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函數本身。

有點頭暈,看吧:

 

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

 

代碼function Person(name)   
{   
   this.name=name;   
   this.showMe=function()   
        {   
           alert(this.name);   
        }   
};   
  
var one=new Person(‘js‘);   
  
alert(one.prototype)//undefined   
alert(typeof Person.prototype);//object   
alert(Person.prototype.constructor);//function Person(name) {...};  

 


 

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

我們接著看代碼:

 

代碼function Person(name)   
{   
   this.name=name;   
   this.showMe=function()   
        {   
           alert(this.name);   
        }   
};   
  
Person.prototype.from=function()   
{   
  alert(‘I come from prototype.‘);   
}   
  
var one=new Person(‘js‘);   
  
one.showMe();//js,這個結果沒有什麼好奇怪的   
one.from();//I come from prototype.,這個結果有一點奇怪吧  

 

 

要解釋這個結果就要仔細研究一下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中的原型繼承了。

又頭暈了,吧!

 

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

 

 

代碼function Person(name)   
{   
   this.name=name;   
   this.showMe=function()   
        {   
           alert(this.name);   
        }   
};   
  
Person.prototype.from=function()   
{   
  alert(‘I come from prototype.‘);   
}   
  
var one=new Person(‘js‘);   
  
one.showMe();//js,這個結果沒有什麼好奇怪的   
one.from();//I come from prototype.,這個結果有一點奇怪吧   
alert(one.constructor);//function Person(name) {...}   
alert(Person.prototype.constructor);//function Person(name) {...}  

 

 


 


 接著看繼承是如何?的。

 

代碼function Person(name)   
{   
   this.name=name;   
   this.showMe=function()   
        {   
           alert(this.name);   
        }   
};   
  
Person.prototype.from=function()   
{   
  alert(‘I come from prototype.‘);   
}   
  
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) {...};  

 


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

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

 

 

 

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

 

代碼function Person(name)   
{   
   this.name=name;   
   this.showMe=function()   
        {   
           alert(this.name);   
        }   
};   
  
Person.prototype.from=function()   
{   
  alert(‘I come from prototype.‘);   
}   
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(){...}  
 

 



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

JS中的繼承了吧。

 

 

 

 

摘自:http://blog.csdn.net/niuyongjie/archive/2009/11/15/4810835.aspx

最初對js中 object.constructor 的認識:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.