Home > Web Front-end > JS Tutorial > body text

javascript arguments usage examples_javascript tips

WBOY
Release: 2016-05-16 16:26:20
Original
1025 people have browsed it

Copy code The code is as follows:




Copy code The code is as follows:




1. In JavaScript, the arguments object is a special object. It is actually a built-in property of the current function. arguments is very similar to Array, but is not actually an Array instance. This can be confirmed by the following code (of course, in fact, in the function funcArg, it is not necessary to write funcArg.arguments when calling arguments, just write arguments directly).

Copy code The code is as follows:
Array.prototype.testArg = "test";
function funcArg() {
alert(funcArg.arguments.testArg);
alert(funcArg.arguments[0]);
}
alert(new Array().testArg); // result: "test"
funcArg(10); // result: "undefined" "10"

2. The length of the arguments object is determined by the number of actual parameters rather than the number of formal parameters. Formal parameters are variables that are re-opened in memory space within the function, but they do not overlap with the memory space of the arguments object. When both arguments and values ​​exist, the two values ​​are synchronized, but when one of them has no value, the value will not be synchronized for this valueless case. The following code can be verified.

Copy code The code is as follows:
function f(a, b, c){
alert(arguments.length); // result: "2"
a = 100;
alert(arguments[0]); // result: "100"
arguments[0] = "qqyumidi";
alert(a); // result: "qqyumidi"
alert(c); // result: "undefined"
c = 2012;
alert(arguments[2]); // result: "undefined"
}
f(1, 2);

3. From the declaration and calling characteristics of functions in JavaScript, it can be seen that functions in JavaScript cannot be overloaded.

According to the basis for overloading in other languages: "The function return value is different or the number of formal parameters is different", we can draw the above conclusion:

First: The declaration of a Javascript function does not have a return value type;

Second: Strictly speaking, the number of formal parameters in JavaScript is only to facilitate variable operations in functions. In fact, the actual parameters are already stored in the arguments object.

In addition, let’s deeply understand why functions in JavaScript cannot be overloaded from the JavaScript function itself: In JavaScript, functions are actually objects, and the function name is a reference to the function, or the function name itself is a variable. For the function declaration and function expression shown below, the meaning is the same as above (without considering the difference between function declaration and function expression), which is very helpful for us to understand the feature that functions in JavaScript cannot be overloaded. .

Copy code The code is as follows:

function f(a){
Return a 10;
}
function f(a){
Return a - 10;
}
// Without considering the difference between function declaration and function expression, it is equivalent to the following
var f = function(a){
Return a 10;
}
var f = function(a){
Return a - 10;
}

4. There is a very useful attribute in the arguments object: callee. arguments.callee returns the current function reference in which this arguments object resides. It is recommended to use arguments.callee instead of the function name itself when using recursive function calls.

is as follows:

Copy code The code is as follows:

function count(a){
If(a==1){
          return 1;
}  
Return a arguments.callee(--a);
}
var mm = count(10);
alert(mm);
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!