【拾遺】理解Javascript中的Arguments

前言

最近在看JavaScript相關的知識點,看到了老外的一本Javascript For Web Developers,遇到了一個知識盲點,以爲老外寫的很明白很透徹,記錄下來加深印象,下面是我摘出來的一些片斷,片斷下有對應的解釋,但願也能幫助其餘人掃除這個盲點。若有翻譯的不得體的地方還請在評論區指出,不勝感激。html

理解Javascript中的Arguments

Function arguments in ECMAScript don’t behave in the same way as function arguments in most other languages. An ECMAScript function doesn’t care how many arguments are passed in, nor does it care about the data types of those arguments. Just because you define a function to accept two arguments doesn’t mean you can pass in only two arguments. You could pass in one or three or none, and the interpreter won’t complain. This happens because arguments in ECMAScript are represented as an array internally. The array is always passed to the function, but the function doesn’t care what (if anything) is in the array. If the array arrives with zero items, that’s fine; if it arrives with more, that’ s okay too. In fact,there actually is an arguments object that can be accessed while inside a function to retrieve the values of each argument that was passed in.微信

Function arguments在ECMAScript中的行爲並不像其餘大多數語言中的函數參數。在ECMAScript中,function並不關心有多少個參數傳入函數中,也不關心傳入參數的數據類型。當你定義了一個有兩個參數的函數時,並不意味着你必定要傳遞兩個參數,你也能夠傳入一個或者三個甚至是不傳,這並不影響函數的解釋。發生這種現象的緣由是在ECMAScript中的arguments表明的是一個內部的array,這個array有0個元素是能夠的,包含多個元素也是能夠的。實際上,在ECMAScript中有一個arguments對象,當function有參數傳入的時候,能夠根據索引去獲取這個arguments對應的值。app

The arguments object acts like an array (though it isn’t an instance of Array ) in that you can access each argument using bracket notation (the first argument is arguments[0] , the second is arguments[1] ,and so on) and determine how many arguments were passed in by using the length property. In theprevious example, the sayHi() function’s first argument is named name . The same value can be accessed by referencing arguments[0] . Therefore, the function can be rewritten without naming the arguments explicitly, like this:ide

arguments對象的行爲有些相似於array,可是實際上它並非Array的實例,在arguments中,能夠經過索引的方式獲取對應的值,例如第一個參數是arguments[0],第二個參數是arguments[1]等等,而且能夠根據argumentslength屬性獲取傳入的參數的數量。在以前的例子中,sayHi()函數的第一個參數命名爲name,與之相同的值能夠經過arguments[0]來獲取。所以,function能夠寫成沒有參數的形式,像這樣:函數

function sayHi() {
    console.log("Hello" + arguments[0] + "," + arguments[1]);
}

SayHi("Jim","Have a good day!");

In this rewritten version of the function, there are no named arguments. The name and message arguments have been removed, yet the function will behave appropriately. This illustrates an important point about functions in ECMAScript: named arguments are a convenience, not a necessity. Unlike in other languages, naming your arguments in ECMAScript does not create a function signature that must be matched later on; there is no validation against named arguments.The arguments object can also be used to check the number of arguments passed into the function via the length property. The following example outputs the number of arguments passed into the function each time it is called:微信支付

在這個版本的代碼中,並無被命名的參數,namemessage參數被移除了,可是函數依然會正常執行,這依賴於ECMAScript重要的特性,參數的命名只是爲了方便,並非必須的。不像其餘的語言,定義的函數命名了參數並不必定非要編寫與之簽名一致的函數,也沒有強制驗證命名參數。arguments對象還能夠經過length屬性來檢查傳入的參數的長度,下面的代碼展現了每一個函數被調用時傳入參數的個數:this

function howManyArgs() {
    console.log(arguments.length);
}
howManyArgs("string", 45);  //2
howManyArgs();              //0
howManyArgs(12);            //1

對我來講,之因此這裏是一個知識盲點,或多或少與思惟定勢有些關係,之前老是認爲function的參數和強類型語言是同樣的,怎麼定義的就怎麼傳遞參數。可是,命名參數仍是有好處的,不用經過arguments索引方式獲取參數值。總之,掃除了一個知識盲點。spa

做者:悠揚的牧笛翻譯

博客地址:http://www.cnblogs.com/xhb-bky-blog/p/9361395.html code

聲明:本博客原創文字只表明本人工做中在某一時間內總結的觀點或結論,與本人所在單位沒有直接利益關係。非商業,未受權貼子請以現狀保留,轉載時必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。若是您以爲文章對您有幫助,能夠【打賞】博主或點擊文章右下角【推薦】一下。您的鼓勵是博主堅持原創和持續寫做的最大動力!

微信支付  支付寶支付

相關文章
相關標籤/搜索