//Passed in FF2.0, IE7, Opera9.25, Safari3.0.4 Function==Function.constructor //result: true Function==Function.prototype.constructor //result: true Function==Object.constructor //result: true //Function also equals to Number.constructor, String.constructor, Array.constructor, RegExp.constructor, etc. function fn(){} Function==fn.constructor //result: true
這說(shuō)明了幾個(gè)問(wèn)題: Function指向系統(tǒng)內(nèi)置的函數(shù)構(gòu)造器(build-in Function constructor);Function具有自舉性;系統(tǒng)中所有函數(shù)都是由Function構(gòu)造。
2. 左下角的obj1, obj2...objn范指用類似這樣的代碼創(chuàng)建的對(duì)象: function fn1(){}; var obj1=new fn1();這些對(duì)象沒(méi)有本地constructor方法,但它們將從Prototype鏈上得到一個(gè)繼承的constructor方法,即fn.prototype.constructor,從函數(shù)對(duì)象的構(gòu)造過(guò)程可以知道,它就是fn本身了。
3.右下角的obj1, obj2...objn范指用類似這樣的代碼創(chuàng)建的對(duì)象: var obj1=new Object();或var obj1={};或var obj1=new Number(123);或obj1=//w+/;等等。所以這些對(duì)象Prototype鏈的指向、從Prototype鏈繼承而來(lái)的 constructor的值(指它們的constructor是build-in Number constructor還是build-in Object constructor等)等依賴于具體的對(duì)象類型。另外注意的是,var obj=new Object(123);這樣創(chuàng)建的對(duì)象,它的類型仍然是Number,即同樣需要根據(jù)參數(shù)值的類型來(lái)確定。同樣它們也沒(méi)有本地constructor,而是從Prototype鏈上獲得繼承的constructor方法,即build-in *** constructor,具體是哪一個(gè)由數(shù)據(jù)類型確定。 示例代碼
//自定義對(duì)象代表,對(duì)應(yīng)Javascript Object Model中的use defined functions function Foo(){} //自定義對(duì)象創(chuàng)建的對(duì)象實(shí)例的代表,對(duì)應(yīng)Javascript Object Model中的objects that created by user defined functions var foo = new Foo(); //String內(nèi)置函數(shù)代表 //str為內(nèi)置函數(shù)創(chuàng)建的對(duì)象實(shí)例的代表,對(duì)應(yīng)Javascript Object Model中的objects that created by build-in constructors var str = new String("string");