簡(jiǎn)單了解JavaScript arguement原理及作用
問題
var length = 10;function fn(){ alert(this.length);}var obj = { length: 5, method: function(fn) { arguments[0]() }}obj.method(fn);//1
這段代碼中的arguments[0]()是第一個(gè)參數(shù)?帶一對(duì)小括號(hào)是什么意思?
理解
我們可以先從最后調(diào)用obj.method(fn)開始理解。
1.obj是對(duì)象,method()是obj的方法,fn是method()的參數(shù),fn是函數(shù)的名,他引用對(duì)應(yīng)的函數(shù)。arguments是JavaScript的一個(gè)內(nèi)置對(duì)象。
An Array-like object corresponding to the arguments passed to a function.The arguments object is a local variable available within all functions; arguments as a property of Function can no longer be used. Description:You can refer to a function‘s arguments within the function by using the arguments object. This object contains an entry for each argument passed to the function, the first entry’s index starting at 0.
2.arguments是用來取得method(fn)的參數(shù)的類數(shù)組,在這里也就是fn,即arguments[0]===fn或arguments.0===fn(0就是arguments的一個(gè)屬性)。所以arguments[0]()就等于fn()。
是不是到這里要開始風(fēng)中凌亂了,this.length究竟是指向那個(gè)對(duì)象呢? 可以這樣理解:
arguments = { 0: fn, //也就是 functon() {alert(this.length)} 1: 第二個(gè)參數(shù), //沒有 2: 第三個(gè)參數(shù), //沒有 ..., length: 1 //只有一個(gè)參數(shù)}
最后,這個(gè)1就是arguments.length,也就是本函數(shù)參數(shù)的個(gè)數(shù)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. nestjs實(shí)現(xiàn)圖形校驗(yàn)和單點(diǎn)登錄的示例代碼2. html小技巧之td,div標(biāo)簽里內(nèi)容不換行3. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式4. laravel ajax curd 搜索登錄判斷功能的實(shí)現(xiàn)5. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))6. python實(shí)現(xiàn)自動(dòng)化辦公郵件合并功能7. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法8. python開發(fā)飛機(jī)大戰(zhàn)游戲9. Echarts通過dataset數(shù)據(jù)集實(shí)現(xiàn)創(chuàng)建單軸散點(diǎn)圖10. css進(jìn)階學(xué)習(xí) 選擇符
