Home  >  Article  >  Web Front-end  >  Learning arguments in Javascript

Learning arguments in Javascript

高洛峰
高洛峰Original
2016-12-13 17:55:571296browse

arguments is a parameter of the currently executing function, which saves the parameters of the current call of the function.

Usage: function.arguments[i].

Where function. is optional and is the name of the function currently being executed.

arguments cannot be created. They are parameters of the function itself and can only be used when the function starts executing.

Although the usage of arguments is very similar to an array, it is not an array.

Below, use an example to demonstrate:

function argumentsTest (a,b) {
        alert(typeof arguments);
}
argumentsTest(1,2);

You can see that this is a pop-up in the browser window, and the argument type is object. The pop-up result is 4.

The following is the callee method, which returns the function object being executed. Learning arguments in Javascript

function argumentsTest (a,b) {
        // alert(typeof arguments);
        alert(arguments.length);
}
argumentsTest(1,2);

Pop-up result:

Learning arguments in Javascript

The following is the key, what is the value returned by arguments.callee.length?

function argumentsTest (a,b) {
        // alert(typeof arguments);
        // alert(arguments.length);
        alert(arguments[1]);
}
argumentsTest(1,2);

The pop-up result:

Learning arguments in Javascript

It can be seen that arguments.length returns the length of the actual parameter, which is 4; and arguments.callee.length returns the length of the formal parameter, which is only 2.

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