小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

JavaScript中的不可見數(shù)據(jù)類型

 quasiceo 2015-06-10

JavaScript中的不可見數(shù)據(jù)類型

JS提供了一些內(nèi)置對象、函數(shù)和構造器供我們編程,如Math、parseInt、Object、Array等。這些都是可見的,編程時可以使用的。比如我可以new Object 或 new Array。

 

有一些則是不可見的,這些對象只能由引擎在特殊的情形提供。這些類型的對象往往還被消減了一些功能。下面列舉一些

 

一、Arguments 類型

Arguments 類型 不能由程序員手動創(chuàng)建其對象,即你不能 new Arguments() 。 它有且僅有一個對象arguments

1
2
3
4
5
function func() {
    console.log(arguments[0])     // 1
    console.log(arguments.length) // 3
}
func(1, 2, 3)

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ù)。

1
2
3
4
5
6
function func(age) {
    console.log('name: ' + this.name + ', career: ' + age)
}
var person = {name: 'John McCarthy'}
var f1 = func.bind(person, 'computer scientist')
f1() // name: John McCarthy, career: computer scientist

可以看到返回的函數(shù)f1和普通函數(shù)一樣使用小括號執(zhí)行調(diào)用了。 一切正常,但下面的代碼會讓你大跌眼鏡

1
2
3
4
5
6
function func(age) {
    console.log('name: ' + this.name + ', career: ' + age)
}
var person = {name: 'John McCarthy'}
var f1 = func.bind(person, 'computer scientist')
console.log(f1.prototype) // undefined

和上面代碼比較,就最后一句不同,沒有執(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

 

 

三、大家補充...

 

 

 

posted on 2013-11-20 07:14 snandy 閱讀(737) 評論(6) 編輯 收藏

評論

#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  

@woshikay
prototype具體指什么?如果是構造器的prototype,有的是Object的實例,有的則是Function的實例。

F12有的是?煩請列舉。
  

#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;
}
}

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多