Understanding Javascript_14 _ function form parameters and arguments

Source: Internet
Author: User

Note: Before reading this blog post, read "Understanding javascript_13 _ execution model details".
Note: Some of the content in this article is my own inferences, and there is no reference in the official document. If there is an error, I hope to correct it.
Cool code
Let's take a look at a piece of cool code:
Copy codeThe Code is as follows:
Function say (msg, other, garbage ){
Alert (arguments [1]); // world
Var other = 'nice to meet you! ';
Var msg;
Alert (arguments. length );
Alert (msg); // hello
Alert (other); // nice to meet you!
Alert (arguments [1]); // nice to meet you!
Alert (garbage); // undefined
}
Say ('hello', 'World ');

Can you correctly explain the code execution results? Think about it.
I think the result of code running should be very different from what you think! Why is the normal output of msg hello instead of undefined? What happens if the parameter defined by the function is repeated with the variable defined in the function? What is the relationship between arguments and parameters in function definition? Let's answer one by one:
Simple memory Diagram

Note: The dotted line indicates a reference point.

Answer
First, let's understand two concepts: formal parameters and actual parameters. Formal parameters refer to the parameters explicitly specified when a method is defined. Due to the flexibility of the Javascript language, when javascript does not require a method call, the number of parameters passed is the same as that of formal parameters. the actual parameters passed when javascript is actually called are actual parameters. Arguments refers to the actual parameter. As can be seen from the say method, say defines three form parameters, and only two values are passed in actual calls. So arguments. the value of length is 2 instead of 3. next, let's take a look at the special behavior of arguments. In my opinion, arguments treats all actual parameters as objects, the actual parameters of the basic data type are converted to the corresponding object type. This is determined by defining a variable with the same name as a formal parameter in the function and assigning a value. The value of arguments is changed accordingly.
Next, let's analyze the process of building the context for executing the say method. Because the logic is complicated, here I will write some 'pseudocode' to describe it:
Copy codeThe Code is as follows:
Function say (msg, other, garbage ){
// Perform 'pre-resolution' on the variables declared by the function first and execute the internal process. It is invisible.
Var msg = undefined;
Var other = undefined;
Var garbage = undefined;
// Perform 'pre-resolution' on the variables defined inside the Function'
Var other = undefined; // obviously, this definition is meaningless.
Var msg = undefined; // meaningless
// Assign values to actual parameters
Msg = new String ('hello'); // all actual parameters of arguments are treated as objects.
Other = new String ('World ');
// Enter the Function Code Section
Alert (arguments [1]); // world
Other = 'nice to meet you! ';
// Var msg; this is pre-parsed and will not be executed again
Alert (arguments. length); // 2
Alert (msg); // hello
Alert (other); // nice to meet you!
Alert (arguments [1]); // nice to meet you!
Alert (garbage); // undefined
}

This code can already explain all the problems. I won't say much about it.
The only emphasis is that using var internally to define variables with the same name as the formal parameters is meaningless, because after the program 'preresolution', they will be viewed as the same variable.

Others
There are many other features of arguments. I mentioned arguments in "pseudo array". If you are interested, please take a look. You can also refer to this article for the actual application of arguments:
Http://www.gracecode.com/archives/2551/

That's all. I hope you can give me more comments.

Related Article

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.