| JavaScript中的不可見數(shù)據(jù)類型JS提供了一些內(nèi)置對象、函數(shù)和構造器供我們編程,如Math、parseInt、Object、Array等。這些都是可見的,編程時可以使用的。比如我可以new Object 或 new Array。 
 有一些則是不可見的,這些對象只能由引擎在特殊的情形提供。這些類型的對象往往還被消減了一些功能。下面列舉一些 
 一、Arguments 類型Arguments 類型 不能由程序員手動創(chuàng)建其對象,即你不能 new Arguments() 。 它有且僅有一個對象arguments 
 arguments對象是在函數(shù)調(diào)用的時候創(chuàng)建的,只在函數(shù)內(nèi)部可見和使用。可以看到arguments很象Array,可以按索引取元素,也有l(wèi)ength屬性。但它不是Array,它沒有Array的有一些方法比如push、pop等。Arguments 在ES5 10.6 里定義。 
 二、bind返回的函數(shù)很特殊bind是ES5給Function.prototype新增的一個方法,它和call/apply一樣在function上直接調(diào)用。它返回一個指定了上下文和參數(shù)的函數(shù)。 
 可以看到返回的函數(shù)f1和普通函數(shù)一樣使用小括號執(zhí)行調(diào)用了。 一切正常,但下面的代碼會讓你大跌眼鏡 
 和上面代碼比較,就最后一句不同,沒有執(zhí)行f1(),而是打印出f1.prototype,發(fā)現(xiàn)是undefined。 奇怪嗎? 每個function不都有一個prototype屬性嗎,這是用來實現(xiàn)原型繼承的哦。的確,bind返回的function比較特殊,它沒有prototype。這種特殊的函數(shù)是由JS引擎創(chuàng)建的,客戶端程序員沒法通過函數(shù)聲明或函數(shù)直接量得到。 
 這一點在規(guī)范里有明確提示 ES5 15.3.4.5 
 
 
 三、大家補充...
 
 
 分類: JavaScript 評論#1樓 2013-11-20 09:55 zmcnxd
 不可見“數(shù)據(jù)類型”這個叫法似乎不妥 #2樓[樓主] 2013-11-20 11:17 snandy
 @zmcnxd 應該叫什么? #3樓 2014-02-08 16:06 woshikay
 還有portotype等等等按下F12有的是~ #4樓[樓主] 2014-02-08 22:23 snandy
 #5樓 2014-02-08 22:53 woshikay
 @snandy 是啊~OBJECT或者是FUNCTION的還有一個什么構造函數(shù)~也是每個都帶的~在chrome里F12然后檢測一個一個變量的值就會出現(xiàn)很多很多的屬性~有的可以用有的不可以用~ #6樓 2015-01-12 01:12 JS牡丹
 請參考如下 bind 方式 if( !Function.prototype.bind2 ){ Function.prototype.bind2 = function( obj ){ if ( typeof obj !== "object" && typeof obj !== "function" ){ return false; } var args = arguments, obj = obj === this || obj instanceof this ? null : obj || window, methodSlice = [].slice, aAgs1 = methodSlice.call( args, 1 ), _this = this, tempFn = function(){ }, bindedFn = function(){ return _this.apply( obj, aAgs1.concat( methodSlice.call( arguments ) ) ) }; tempFn.prototype = this.prototype; bindedFn.prototype = new tempFn(); return bindedFn; } } | 
|  |