JavaScript arguments and JavaScript function overloads

Source: Internet
Author: User
Tags array object count function definition functions implement variables return

1. All functions have a arguments object of their own, which includes the parameters to be invoked by the function. He is not an array, and if you use typeof arguments, it returns ' object '. Although we can call the arguments using the method that invokes the data. such as length, and the index method. However, the push and pop objects of the array are not applicable.

2. There is no relationship between the number of parameters in the function definition and the number of arguments in the function call. The first and second arguments passed in in a function can be used with f.arguments[0] and f.arguments[1], arguments cannot be created, are parameters of the function itself, and can only be used if the function begins to execute.
Although the use of arguments is much like an array, it is not an array. The length of the arguments object is determined by the number of arguments, not 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.


function Argumentstest (a,b) {
Alert (typeof arguments);
alert (arguments.length);
Alert (arguments[1]);
}
Argumentstest (1,2,3,4);

The output results are 4, 2;

function Argumentstest (a,b) {
alert (Arguments.callee);
alert (arguments.callee.length);
}
Argumentstest (1,2,3,4);

The result of the output is argumentstest,2;

3. The Declaration and invocation attributes of functions in JavaScript allow you to 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.

How do I implement a JavaScript overload?

Use different methods to implement function overloading using the number of arguments decision parameters:

<script language= "JavaScript" >
function f (length)
{
var len= arguments.length;
if (1 = len)
{
var width = arguments[1];
Alert ("High": "+length+", Wide for: "+width);
"   
else
{
alert ("High:" +length);
}
}
</srcipt>

There is a very useful property in the 4.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.

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

var mm = count (ten);
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: info-contact@alibabacloud.com 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.