The usage of arguments in JS __js

Source: Internet
Author: User

1. In JavaScript, the arguments object is a very special object and is actually a built-in property of the current function. Arguments is very similar to array, but is not actually an array instance. Can be confirmed by the following code (of course, in fact, in the function funcarg, call arguments is not necessary to write a funcarg.arguments, directly written arguments).

function Funcarg () {
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 arguments rather than the number of parameters. A formal parameter is a variable that arguments the memory space inside a function, but it does not overlap with the memory space of the object. For both arguments and values, the values are synchronized, but for one of them that has no value, the value of the condition is not synchronized. The following code can be validated.

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

But arguments and formal parameters interact with each other, for example, if the arguments[0]= "Hello" is modified, the value of a is also changed to Hello.

3. By declaring and invoking features of functions in JavaScript, you can see that functions in JavaScript are not overloaded.

Depending on the overload in other languages: "The function return value is different or the number of parameters is different", we can draw the conclusion that:

First: The declaration of a JavaScript function has no return value type;

Second: The number of formal parameters in JavaScript is strictly in order to facilitate the operation of variables in the function, actually the argument is already stored in the arguments object.

In addition, the JavaScript function itself is an in-depth understanding of why functions in JavaScript cannot be overloaded: In JavaScript, functions are objects, function names are references to functions, or function names themselves are variables. The function declarations and function expressions shown below are actually the same (without considering the difference between a function declaration and a function expression), which is very helpful in understanding the feature that functions in JavaScript are not overloaded. function f (a) {
return a + 10;

function f (a) {
return a-10;

Without considering the difference between a function declaration and a function expression, it is equivalent to the following

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

var f = function (a) {
return a-10;

JavaScript does not have the ability to overload functions, but arguments objects can simulate overloading.

function Hi () {
if (arguments[0]== "Andy") {return
Alert (arguments[0]);

4, there is a very useful property in the arguments object: callee. Arguments.callee returns the current function reference for this arguments object. It is recommended to use Arguments.callee instead of the function name itself when using recursive calls to functions. Represents a reference to the function object itself, which is the body of the specified function object, which facilitates the recursive implementation of the Nameless function or the encapsulation of the function.

function count (a) {
if (a==1) {
return 1;
Return a + Arguments.callee (--a);

var mm = count (10);
alert (mm);

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.