Home  >  Article  >  Web Front-end  >  javascript arguments implicit parameters passed to functions_javascript tips

javascript arguments implicit parameters passed to functions_javascript tips

WBOY
WBOYOriginal
2016-05-16 18:47:51781browse

When I first saw this stuff, I thought it was a "disguised" ordinary parameter, but it didn't need to be explicitly declared when defining the function.
However, Code is cheap.) Look at the code:
function funcTest(a, b) {
alert(a);
alert(b);
for ( var i = 0 ; i alert(arguments[i]);
}
}
function test() {
funcTest( 1 , 2 , 3); // Put it in the html page for execution
}
function funcTest(a, b) {
alert(a);
alert(b);
for ( var i = 0 ; i alert(arguments[i]);
}
}
function test() {
funcTest( 1 , 2 , 3 ); // Put it into the html page for execution
}
How does it look after the code is run? Yes, the length here is really weird, right? Let’s see what the book says, “When the code is running, it will be displayed in sequence: 1, 2, 1, 2, 3. Therefore, when defining a function, even if the parameter list is not specified, the obtained parameters can still be referenced through arguments. Parameters, which brings a lot of flexibility to programming." There is no more incisive explanation than this sentence.
Seeing this, we may all have a question (if you have a little knowledge of js programming), is the arguments parameter an instance of a js array object? The following code will help you solve this doubt:
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();
}
Code The running result is that "test arguments" and "undefined" pop up first. Why does the funcArg call return "undefined"? After reading this, I believe your questions about the above have been eliminated.
Do you think you have mastered all arguments at this point? That would be too much to underestimate the talented designer(s) of js. 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 in 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 in 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 into 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 in the html page for execution
}
Both answers A1 and A2 solved the problem, I believe the first one This method is the common practice for most people, but JS recommends using the second method. The original book says that this method A1 "in which the function contains a call to sum itself. However, for JavaScript, the function name is just a variable name. Calling sum inside a function is equivalent to calling a global variable, which cannot be well reflected as calling itself. "sum has called sum, and it is said that "it cannot be well reflected that it is calling itself". Why?
You will regret less when you use the book. Check the book and it says this: "Another attribute of the arguments object is callee, which represents a reference to the function object itself, which is helpful for implementing recursion of unnamed functions or guaranteed functions. "Encapsulation," I admit that this is reasonable. I have always maintained a high degree of trust in books, especially technical books, but here it is said that "another attribute of the arguments object is callee". How does "arguments" become " "Object"? The title says "Implicit parameters passed to functions: arguments". Could it be that I copied it wrongly? Check out the e-book, damn, can you still make mistakes when copying and pasting?
Object, object? There are so many objects, let’s look for “objects” in the next article.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn