【拾遺】理解Javascript中的Arguments

發布日期:2019-06-25

前言

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

理解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對應的值。

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:

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屬性來檢查傳入的參數的長度,下面的代碼展示了每個函數被調用時傳入參數的個數:

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

對我來說,之所以這里是一個知識盲點,或多或少與思維定勢有些關系,以前總是認為function的參數和強類型語言是一樣的,怎么定義的就怎么傳遞參數。但是,命名參數還是有好處的,不用通過arguments索引方式獲取參數值。總之,掃除了一個知識盲點。

作者:悠揚的牧笛

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

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

??

pc蛋蛋在线下载