簡單了解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]()是第一個參數(shù)?帶一對小括號是什么意思?
理解
我們可以先從最后調(diào)用obj.method(fn)開始理解。
1.obj是對象,method()是obj的方法,fn是method()的參數(shù),fn是函數(shù)的名,他引用對應(yīng)的函數(shù)。arguments是JavaScript的一個內(nèi)置對象。
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的一個屬性)。所以arguments[0]()就等于fn()。
是不是到這里要開始風(fēng)中凌亂了,this.length究竟是指向那個對象呢? 可以這樣理解:
arguments = { 0: fn, //也就是 functon() {alert(this.length)} 1: 第二個參數(shù), //沒有 2: 第三個參數(shù), //沒有 ..., length: 1 //只有一個參數(shù)}
最后,這個1就是arguments.length,也就是本函數(shù)參數(shù)的個數(shù)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 基于javaweb+jsp實現(xiàn)學(xué)生宿舍管理系統(tǒng)2. 多級聯(lián)動下拉選擇框,動態(tài)獲取下一級3. 如何封裝一個Ajax函數(shù)4. ASP.NET MVC實現(xiàn)樹形導(dǎo)航菜單5. Spring security 自定義過濾器實現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實例代碼)6. Java 接口和抽象類的區(qū)別詳解7. springboot返回modelandview頁面的實例8. python怎么運(yùn)行代碼9. PHP擴(kuò)展之URL編碼、解碼及解析——URLs10. Laravel Eloquent ORM高級部分解析
