Scope of function: calling ObjectThe body of a function in JavaScript is performed in a local scope, which differs from the global scope. This new scope is created by adding the calling object to the head of the scope chain (this sentence is not understood, the understanding of the pro can leave a message to tell me, thank you). Because the calling object is part of the scope chain, the object property can be accessed as a variable in the body of the
This article mainly introduces the relevant information about the arguments object in Javascript, which is very good and has reference value. If you need it, you can refer to all objects in js and even functions are objects, the function name is actually a variable that references the function definition object.
1. What is arguments?
The
JavaScript arguments to implement variable-length parameters.
In C #, there are variable long parameter params[], but in JS, how to implement this variable parameter?
Variable-length parameters
Arguments is a very good solution and has never known that JavaScript has this thing.
First take a look at the application
At noon, I studied arguments in javascript Functions.Copy codeThe Code is as follows: The arguments. length and arguments. callee are observed.First, arguments makes sense only in the function body. arguments. length returns the n
Arguments object in Javascript, javascriptarguments
In js, everything is an object, and even a function is an object. A function name is actually a variable that references a function definition object.
1. What is arguments?
The arguments in this function is very special. It is actually a built-in class array object of
external access to this object will be wrong or only nullarguments wonderful Behavior 1 because it is an object of the class array, it has a length attribute and can use subscript to access the parameter list. 2 because it is a function attribute, if you re-define an arguments variable in the function body, it will overwrite the original arguments.Code:
Function foo (a, B, c ){
Var arguments = 1;
is the primary)function box (num) { return num +;} function box (num) { // will execute this functions return num + +;} Alert (box); // The returned result is 250, which is called the second functionfunction Box (num,a) { return num +;} function box (num) { // will execute this functions return num + +;} Alert (Box (50,3)); // The result also indicates whether to execute the second function
since the JS call function can not be in accordance with the function of the definition format to pass parameters, for example: function Test (A, b) {do something} Test (1); Test (from); Test (); The above three call methods are correct, but if the test function uses a A, b parameter, the test () Call function may return a undefined error . So JS provides a keyword arguments to get the parameters passed in. Argu
(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
. 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 functio
]);} Argumentstest (1,2,3,4);Popup Result: The result of the popup is 4. Here is the callee method, which returns the function object that is being executed.function Argumentstest (A, b) { //alert (typeof arguments); alert (arguments.length); Alert (arguments[1]); alert (Arguments.callee); alert (arguments.callee.length);} Argumentstest (1,2,3,4);Popup Result: The following is the key, argume
theFunctionObject function the body of the object. The callee property is a member of the arguments object and is available only if the related function is executing. callee The initial value of the property is the that is being executed; function object, which allows anonymous recursive functions. var function (n) { if (1 = = N) {return 1; Else { return n + arguments.callee (n-1); } }
function sum (a) {
Console.log (a*a) is evaluated;
If the user passes in two parameters, sum
function sum (a,b) {
console.log (a+b);
}
SUM (4); ?
In the example above, the idea is to have the same function sum () different output depending on the parameters, but sum is the function name, the essence is a variable,
The second one will overwrite the first, so the correct output of the above answer is: nan,9. So it's obviously not possible.
If you use
Because of the error in language design, arguments can be treated as an array.
Copy Code code as follows:
function zero () {
Console.log (Arguments[0]);
}
There will also be
Copy Code code as follows:
function zero () {
for (Var i=0;iConsole.log (Arguments[i]);
}
}
It leverages the fact that
The arguments object of JavaScript is a simple introduction:Here is the simplest way to introduce the use of the arguments object, first look at a piece of code:var a=1; var b=2; function MyTest (b) { var c=a+B; document.write (arguments[0]); } mytest;The above code can output the first argument passed to the functi
function Add (n1,n2) {return n1+n2;}function Add (n1,n2,n3) {return n1+n2+n3;}alert (add); The//nan,js method uses the nearest principle, and since the method does not pass in N3, the result is NaN
JS there is no method overload, how to solve the above problem?
//argumentsFunction F1 () {var sum=0;for (Var i=0;iSum+=arguments[i];}return sum;}Alert (F1 (1,2,3,4,5));
Using arguments, the
The parameter object arguments in Javascript functions is an object, not an array. However, it can access the elements in the array through the following numeric table, and it also has the length attribute to identify the number of its elements. Use the arguments object to verify whether the function parameters are valid.
Script function sum (arg1, arg2) //
One, Arguments.callee//Gets the function that is currently executing, that is, the function itself, often used to get the anonymous function itselfSyntax: Arguments.callee function (x) { if (x ) { return 1Elsereturn x * Arguments.callee (x-1// return Back to 6 3*2*1 Second, arguments.length//Get the number of arguments passed to the functionSyntax: arguments.length function (x) { return argumen
For those of you who have learned Java. function overloading is not an unfamiliar concept, but is there a function overload in JavaScript ... And then we're going to test it.This case is modeled after the syntax in Java to simulate function overloading. Let's do the results.Alert (ON):Alert (+/-):Results comparison found that the result of the first execution is Nan, and the result of the second execution is normal.There is no phenomenon of function o
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.