您的位置:首頁技術文章
文章詳情頁

簡單了解JavaScript arguement原理及作用

瀏覽:107日期:2023-10-25 09:28:55

問題

var length = 10;function fn(){ alert(this.length);}var obj = { length: 5, method: function(fn) { arguments[0]() }}obj.method(fn);//1

這段代碼中的arguments[0]()是第一個參數?帶一對小括號是什么意思?

理解

我們可以先從最后調用obj.method(fn)開始理解。

1.obj是對象,method()是obj的方法,fn是method()的參數,fn是函數的名,他引用對應的函數。arguments是JavaScript的一個內置對象。

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)的參數的類數組,在這里也就是fn,即arguments[0]===fn或arguments.0===fn(0就是arguments的一個屬性)。所以arguments[0]()就等于fn()。

是不是到這里要開始風中凌亂了,this.length究竟是指向那個對象呢? 可以這樣理解:

arguments = { 0: fn, //也就是 functon() {alert(this.length)} 1: 第二個參數, //沒有 2: 第三個參數, //沒有 ..., length: 1 //只有一個參數}

最后,這個1就是arguments.length,也就是本函數參數的個數。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: JavaScript
相關文章:
国产综合久久一区二区三区