【拾遺】理解Javascript中的Arguments

來源:互聯網
上載者:User

標籤:驗證   any   最大   hello   app   直接   size   time   bmp   

前言

最近在看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 

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

  

【拾遺】理解Javascript中的Arguments

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.