Function object of JavaScript learning notes, learning notes function
In JavaScript, function functions are objects.
JS does not support method overloading.
In JavaScript, there is no concept of method (function) overloading.
Example:
Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
Function add (number)
{
Alert (number + 20 );
}
Function add (number, number1)
{
Alert (number + 30 );
}
Add (10 );
</Script>
</Head>
<Body>
</Body>
</Html>
The dialog box on the webpage Shows 40.
Although the second method is two parameters, it is still called.
After the order of the two methods is switched, the pop-up box shows 30. We can see that no matter the number of parameters, the method after the same name is called.
How can this problem be explained?
This is because the function declaration actually creates an object:
Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
Function add (number)
{
Alert (number + 20 );
}
/*
The above functions are equivalent:
Var add = function (number)
{
Alert (number + 20 );
}
*/
Function add (number, number1)
{
Alert (number + 30 );
}
/*
The above functions are equivalent:
Var add = function (number, number1)
{
Alert (number + 30 );
}
*/
Add (10 );
</Script>
</Head>
<Body>
</Body>
</Html>
In this way, add actually points to the following objects, and the parameters assigned during method calling will be assigned to the method parameters in order, and the parameters not assigned subsequently are undefined.
The JavaScript function does not check the number of parameters strictly. it is acceptable that the number of real parameters is smaller than the number of tangible parameters. The unspecified parameter is undefined.
It is also possible that the number of real parameters is greater than the number of form parameters, so that only the preceding real parameters are used, and the extra real parameters are not used.
Function object
There is a Function object in JavaScript. All user-defined functions are Function object types.
All parameters received by the Function object are of the string type. The last parameter is the Function body to be executed, and the preceding parameter is the parameter that the Function actually needs to receive.
Example:
Copy codeThe Code is as follows:
<Html>
<Head>
<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>
</Head>
<Body>
</Body>
</Html>
Implicit object arguments
In JavaScript, each function has an implicit object arguments, indicating the parameters actually passed to the function.
Arguments is independent of the formal parameters of the function and the number of parameters.
Arguments has a useful property length, indicating the length of a real parameter. You can use this to simulate the function overload:
Exercise example:
Copy codeThe Code is as follows:
<Html>
<Head>
<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>
</Head>
<Body>
</Body>
</Html>
Each function object has a length attribute, indicating the parameter format that the function expects to receive.
Unlike the arguments function, arguments. length indicates the number of parameters actually received by the function.
Example:
Copy codeThe Code is as follows:
<Html>
<Head>
<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>
</Head>
<Body>
</Body>
</Html>