JavaScript arguments The implicit parameters _javascript techniques for passing to functions
Source: Internet
Author: User
When I first saw this dongdong, I thought it was the normal parameter of "disguise", but I didn't have to declare it clearly when I was defining the function.
However, the code is cheap (the code is cheap.) Looking at the codes:
function Functest (A, b) {
alert (a);
alert (b);
for (var i = 0; i < arguments.length i + +) {
Alert (Arguments[i]);
}
}
function Test () {
Functest (1, 2, 3); Put in HTML page for execution
}
function Functest (A, b) {
alert (a);
alert (b);
for (var i = 0; i < arguments.length i + +) {
Alert (Arguments[i]);
}
}
function Test () {
Functest (1, 2, 3); Put in HTML page for execution
}
How does the code work, yes, the length here is really strange, right? Let's look at what the book says, "The code runs in turn: 1,2,1,2,3." Therefore, when you define a function, you can still refer to the obtained parameter by arguments, even if you do not specify a parameter list, which gives you a lot of flexibility in programming. There is no more incisive explanation than this sentence.
See here, we may all have a doubt (if you have a little bit of JS programming knowledge), arguments parameter is not a JS array object an instance? 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 ();
}
The code run result is to eject "test arguments" and "undefined" first, why Funcarg call return "undefined"? See here believe you have eliminated the question above.
You think you've mastered all of arguments? That is too much to underestimate the genius of JS designers (they), the following look at another talented design:
Q: Calculates the sum of the natural numbers of 1 to n using recursive return
A1:
function sum (n) {
if (1 = n) return 1;
else return n + sum (n-1);
}
function Test () {
Alert (sum (100));; Put in HTML page for execution
function sum (n) {
if (1 = n) return 1;
else return n + sum (n-1);
}
function Test () {
Alert (sum (100));; Put in 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 in 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 in HTML page for execution
}
A1 and A2 Both answered the question, believing that the first method is common practice for most people, but JS recommends using the second, the original book says A1 this Way "where functions contain calls to sum itself, but for JavaScript, the function name is only a variable name, Calling sum within a function is equivalent to calling a global variable, which is not a good representation of the call itself ", Sum called sum, and said" can not be very well reflected in the call itself, "why?
When the book is to hate less, check the book, the book is so written: "Another property of the arguments object is callee, it represents a reference to the function object itself, which is conducive to the implementation of the nameless function of recursion or guarantee the encapsulation of functions," I admit that the reason, I have always maintained a high degree of trust in books, especially technical books, but this says "another attribute of the arguments object is callee", and How does "arguments" become "object"? The title says "the implied parameter passed to the function: arguments", is transcription wrong? Check the ebook, rely on, copy and paste will be wrong?
Object, object? How many objects, the next chapter to find "object."
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