JavaScript arguments using examples _javascript tips

Source: Internet
Author: User

Copy Code code as follows:

<script language= "JavaScript" >
The first parameter value.
function test (a,b,c,d) {
Alert (arguments[0]);
}
Arguments[0] is actually a, similarly, arguments[1] is B, sequentially c,d
</script>


Copy Code code as follows:

<script language= "JavaScript" >
{
function function_name (EXP1,EXP2,EXP3,EXP4)
{
var umber= "";
Umber=arguments.length;
alert (umber);
}
Function_name (' A ', ' B ', ' C ', ' d '); Can call
Function_name ("A", "B", "C", "D"); Can call
}
</script>

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

Copy Code code 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 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.

Copy Code code 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. 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.

Copy Code code as follows:

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

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.

As follows:

Copy Code code as follows:

function count (a) {
if (a==1) {
return 1;
}
Return a + Arguments.callee (--a);
}
var mm = count (10);
alert (mm);

Related Article

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.