Home > Web Front-end > JS Tutorial > JavaScript arguments object

JavaScript arguments object

黄舟
Release: 2016-12-13 09:18:12
Original
939 people have browsed it

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).

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"
Copy after login

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.

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);
Copy after login

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. .

function f(a){
    return a + 10;
}

function f(a){
    return a - 10;
}

// 在不考虑函数声明与函数表达式区别的前提下,其等价于如下

var f = function(a){
    return a + 10;
}

var f = function(a){
    return a - 10;
}
Copy after login

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:

function count(a){
    if(a==1){
        return 1;
    } 
    return a + arguments.callee(--a);
}

var mm = count(10);
alert(mm);
Copy after login


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