In javascript, functions are objects
The code copy is as follows:
<html>
<head>
<script type="text/javascript">
function add(number){
alert(number+20);
}
var add=function(number){
alert(number+20);
}
function add(number,number1){
alert(number+30);
}
var add=function(number){
alert(number+90);
}
add(10);
</script>
</head>
<body>
</body>
</html>
add is a reference, function is an object .
What's different from Java: There is no concept of method overloading in JavaScript. The method can have n parameters, and only 1 parameter can be passed when passing the parameter.
Data type Undefined-type undefined-value
There is a Function object in JavaScript, and all custom functions are of Function object type.
The Function object receives all parameters of string type, the last parameter is the function body, and the previous parameter is the parameters that the function really needs to receive.
The code copy is as follows:
<html>
<head>
<script type="text/javascript">
var add =new Function("number","alert(number+20);");
add(10);
</script>
</head>
<body>
</body>
</html>
In javascript, each Function object has an implicit object arguments, representing the parameters actually passed to the function.
The code copy is as follows:
<html>
<head>
<script type="text/javascript">
function add(){
alert(arguments.length);
alert(arguments[0]);
alert(arguments[1]);
}
add(10,20);
</script>
</head>
<body>
</body>
</html>
Method overloading in java, relative in javascript can also be implemented by arguments.
The code copy is as follows:
<html>
<head>
<script type="text/javascript">
function add(){
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]);
}
}
add(2);
add(2,3);
add(2,3,4);
</script>
</head>
<body>
</body>
</html>
The above is all about this article. Have you understood the JavaScript object model and function object? If you have any questions, please leave a message and make progress together.