JavaScript:理解constructor

5種類型的對象輸出的構造器是5種類型,這是一個簡單的例子:

var a = new Array();

if(a.constructor == Array) return true; ==> true

 var o = new Object();

if(o.constructor == Object) return true; ==> true

 那麼一個函數的構造器,也就是輸出這個函數,沒錯你猜對了,就是這樣的。

function fun(){ } ;

fun.prototype.constructor = fun;

這樣寫,你能看懂嗎?看了下面這個你就懂了。

function obj(){ } ;

obj.prototype = Object.prototype.constructor;

這樣就能區別出繼承的函數是實例對象還是繼承構造函數。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章