Implicit parameters passed to the function by JavaScript arguments

Source: Internet
Author: User

When I first saw this stuff, I thought it was a "disguised" common parameter, but I didn't need to explicitly declare it when defining the function.
However, Code It is cheap (code is cheap.) to view the Code:
Function functest (a, B ){
Alert ();
Alert (B );
For (VAR I = 0; I <arguments. length; I ++ ){
Alert (arguments [I]);
}
}
Function Test (){
Functest (1, 2, 3); // put it on the HTML page for execution
}
Function functest (a, B ){
Alert ();
Alert (B );
For (VAR I = 0; I <arguments. length; I ++ ){
Alert (arguments [I]);
}
}
Function Test (){
Functest (1, 2, 3); // put it on the HTML page for execution
}
How is the code going after running? Good. The length here is really strange, right? Let's take a look at what is said in the book. "The code runs in sequence: 1, 2, 3. Therefore, when defining a function, you can still reference the obtained parameters through arguments even if you do not specify the parameter list. This gives programming great flexibility ". There is no more incisive explanation than this sentence.
Here, we may all have a question (if you have a little JS programming knowledge). Is the arguments parameter an instance of the JS array object? The following code helps you solve this problem:
Array. Prototype. testarg = "test arguments ";
Function funcarg (){
Alert (arguments. testarg );
}
Function Test (){
Alert (new array (). testarg );
Funcarg ();
}
Array. Prototype. testarg = "test arguments ";
Function funcarg (){
Alert (arguments. testarg );
}
Function Test (){
Alert (new array (). testarg );
Funcarg ();
}
The code execution result is "test arguments" and "undefined" first. Why does the funcarg call return "undefined "? I believe that you have eliminated the above questions.
Here you think you have mastered all arguments? That's too small. js's talented designers (who). Let's look at another talented design:
Q: use recursion to calculate the sum of natural numbers from 1 to n
A1:
Function sum (n ){
If (1 = N) return 1;
Else return N + sum (n-1 );
}
Function Test (){
Alert (sum (100); // put it on the HTML page for execution
} Function Sum (n ){
If (1 = N) return 1;
Else return N + sum (n-1 );
}
Function Test (){
Alert (sum (100); // put it on the HTML page for execution
}
A2:
Function sum (n ){
If (1 = N) return 1;
Else return N + arguments. callee (n-1 );
}
Function Test (){
Alert (sum (100); // put it on the HTML page for execution
} Function Sum (n ){
If (1 = N) return 1;
Else return N + arguments. callee (n-1 );
}
Function Test (){
Alert (sum (100); // put it on the HTML page for execution
}
Both A1 and A2 solve the problem. I believe that the first method is the common practice of most people, but JS recommends the second method, the original book said that the A1 Method "in which the function contains calls to sum itself, but for Javascript, the function name is just a variable name, calling sum in a function is equivalent to calling a global variable. It cannot reflect the call itself. "sum calls sum, I also said, "It cannot reflect the call itself." Why?
When books are used, they hate less and check books. This is what the book writes: "another attribute of the arguments object is callee, which indicates reference to the function object itself, this is conducive to recursion of untitled functions or to guarantee function encapsulation. "I admit that I have always maintained a high degree of trust in books, especially technical books, but here, "another attribute of the arguments object is callee". How does "arguments" become an "object? If the title says "the implicit parameter passed to the function: arguments", is it wrong to copy the book? Check the e-book. Why is it wrong to copy and paste it?
Object? Find "object" in the next 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.