In JavaScript, function functions are objects.
No method overload in JS
In JavaScript, there is no concept of method (function) overloading.
Example:
Copy Code code as follows:
<script type= "Text/javascript" >
function Add (number)
{
Alert (number + 20);
}
function Add (number, number1)
{
Alert (number + 30);
}
Add (10);
</script>
<body>
</body>
The pop-up box on the Web page shows 40.
Note Although the second method is two parameters, it is still called.
After swapping the order of two methods, the frame shows 30, and you can see that regardless of the number of parameters, you invoke a method that follows the same name.
How to explain this phenomenon?
This is because the function declaration actually creates an object:
Copy Code code as follows:
<script type= "Text/javascript" >
function Add (number)
{
Alert (number + 20);
}
/*
The above function is equivalent to:
var add = function (number)
{
Alert (number + 20);
}
*/
function Add (number, number1)
{
Alert (number + 30);
}
/*
The above function is equivalent to:
var add = function (number, NUMBER1)
{
Alert (number + 30);
}
*/
Add (10);
</script>
<body>
</body>
So add actually points to the back object, and the parameter given by the method call will be assigned to the method form parameter in order, and the argument without the assigned value is undefined.
JavaScript function call when there is no strict number of parameters to check, the number of arguments is less than the number of formal parameters can be, there is no assigned parameter is undefined value undefined.
It is also possible that the number of arguments is greater than the number of formal parameters, so that only the previous actual participants are used and the extra arguments are not used.
Function object
There is a function object in JavaScript, and all custom functions are function object types.
All parameters received by a function object are of type string, the last of which is the body of the function to execute, and the preceding argument is the parameter that the function really needs to receive.
Example:
Copy Code code as follows:
<script type= "Text/javascript" >
var add = new Function ("number", "Number1", "alert (number + number1);");
var add = new Function ("number", "alert (number + 20);");
Add (10, 30);
</script>
<body>
</body>
Implied object arguments
In JavaScript, each function has an implied object arguments, representing the arguments that are actually passed to the function.
Arguments is independent of the formal parameters of the function and their number.
Arguments has a useful attribute length that represents the length of the argument. You can use this to simulate the overload of a function:
Practice Example:
Copy Code code as follows:
<script type= "Text/javascript" >
function Add (number1, number2)
{
alert (arguments.length);
Alert (arguments[0]);
Alert (arguments[1]);
Alert (arguments[2]);
}
Add (2, 3, 4);
function Add2 ()
{
if (1 = arguments.length)
{
Alert (arguments[0]);
}
else if (2 = arguments.length)
{
Alert (Arguments[0] + arguments[1]);
}
else if (3 = arguments.length)
{
Alert (Arguments[0] + arguments[1] + arguments[2]);
}
}
ADD2 (3);
ADD2 (3, 4);
ADD2 (3, 4, 5);
</script>
<body>
</body>
Each function object has a length property that represents the parameter format that the function expects to receive.
Unlike the arguments of a function, Arguments.length represents the number of parameters actually received by the function.
Example:
Copy Code code as follows:
<script type= "Text/javascript" >
var add = function (num, num2, num3)
{
Alert (num + num2 + num3);
}
alert (add.length); Output 3
Add (1, 2, 3);
var add2 = function ()
{
}
alert (add2.length); Output 0
</script>
<body>
</body>