"Supplements" understands the arguments in JavaScript

Source: Internet
Author: User

Objective

Recently in the reading JavaScript related knowledge point, saw a foreigner of a JavaScript for WEB developers, encountered a knowledge blind spot, feel that the foreigner writes very clearly very thorough, the record to deepen the impression, below is I picked out some fragments, the fragment has the corresponding explanation , hoping to help other people sweep the blind spot. If there is an inappropriate translation of the place please also in the comment area pointed out, greatly appreciated.

Understand the arguments in JavaScript

function arguments in ECMAScript don ' t behave in the same-as function arguments in most other languages. An ECMAScript function doesn ' t care how many arguments is passed in, nor does it care about the data types of those Argum Ents. Just because you define a function to accept the arguments doesn ' t mean you can pass in only the arguments. You could pass in one or three or none, and the interpreter won ' t complain. This happens because arguments in ECMAScript is represented as an array internally. The array is all passed to the function and the function doesn ' t care what (if anything) are 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 are an arguments object, can be accessed and inside a function to retrieve the values of each Argument that is passed in.

The

Function arguments behavior in ECMAScript is not like a function parameter in most other languages. In ECMAScript, function does not care how many arguments are passed into the function, nor does it care about the data type of the incoming parameter. When you define a function with two parameters, it does not mean that you must pass two arguments, you can pass in one or three or even do not, which does not affect the interpretation of the function. This behavior occurs because the arguments in ECMAScript represents an internal array, which has 0 elements that are possible, including multiple elements. In fact, there is a arguments object in the ECMAScript, and when the function parameter is passed in, the arguments corresponding value can be obtained according to the index.

the arguments Object acts like an array (though it isn ' t a instance of array ) in so can access each argument using Bracket notation (the first argument is Arguments[0] , the second was Arguments[1] , and so on) an D 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 accessed by referencing arguments[0] . Therefore, the function can be rewritten without naming the arguments explicitly, like this:

argumentsAn object behaves somewhat like array , but in fact it is not Array an instance, in arguments which the corresponding value can be obtained by means of an index, for example, the first argument is, the arguments[0] second argument is, and so on arguments[1] , and can be based on arguments the length Property gets the number of arguments passed in. In the previous example, the sayHi() first parameter of the function is named name , and the same value can be obtained by passing it arguments[0] . Therefore, function can be written as a form without parameters, like this:

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

Sayhi ("Jim", "a good day!");

in this rewritten version of The function, there is no named arguments. The name and message arguments have been removed, yet the function would behave appropriately. This illustrates an important point is about functions in ecmascript:named arguments is a convenience, not a necessity. Unlike in other languages, naming your arguments in ECMAScript does don't create a function signature that must be matched L Ater on; There is no validation against named arguments. :

In this version of the code, the parameters are not named, name and the message parameters are removed, but the function will still execute normally, depending on the ECMAScript important features, the parameter naming is only for convenience, not necessary. Unlike other languages, a defined function names a parameter and does not necessarily write a function that is consistent with its signature, nor does it force validation of named parameters. An arguments object can also length check the length of an incoming parameter through properties, and the following code shows the number of parameters passed in when each function is invoked:

function Howmanyargs () {    console.log (arguments.length);} Howmanyargs ("string");  // 2Howmanyargs ();              // 0Howmanyargs (n);            // 1

For me, the reason here is a knowledge blind spot, more or less with the thinking set some relationship, previously always thought function parameters and strongly typed language is the same, how to define how to pass parameters. However, it is also advantageous to have a named parameter without obtaining the parameter value by arguments index. In short, swept away a knowledge blind spot.

Melodious Shepherd's flute

Blog Address: http://www.cnblogs.com/xhb-bky-blog/p/9361395.html

Disclaimer: The original text of this blog only represents my work in a certain time to summarize the views or conclusions, and my unit does not have a direct interest in the relationship. Non-commercial, unauthorized posts please retain the status quo, reproduced must retain this paragraph of the statement, and in the article page obvious location to the original connection. If you find the article helpful, you can " reward " The blogger or click "recommend" in the lower right corner of the article. Your encouragement is the greatest motivation for bloggers to insist on original and continuous writing!

"Supplements" understands the arguments in JavaScript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.