In-depth understanding of function overloading in JavaScript, and javascript Overloading
There is a special data type in JavaScript-Function type. Every Function in JavaScript is a Function-type instance. Because the function is an object, the function name is actually a pointer to the function object and will not be bound to a function.
<pre name="code" class="html">function sum(num1,num2) { return num1 +num2; } alert(sum(10,10)); //20 var other = sum; alert(other(10,10)); //20 sum = null; alert(other(10,10)); //20
Using a function name as a pointer to a function helps you understand why ECMAScript does not have the concept of function overloading.
function sum(num1) { return num1 +100; } function sum(num1) { return num1 +200; } alert(sum(200)); //400
Although two functions with the same name are declared, the subsequent functions overwrite the previous functions. The above is equivalent to the following code:
function sum(num1) { return num1 +100; } sum = function(num1) { return num1 +200; } alert(sum(200)); //400
When creating the second function, it actually overwrites the referenced first function variable sum.
Can javascript Functions be overloaded?
The reload of javascript Functions is different from that of java functions.
When defining a JavaScript function, the function name is the identifier of the function object. The number of parameters is only the attribute of the function. It is not feasible to implement overload by defining functions with different numbers of parameters.
When calling a function, js finds the corresponding function object through the function name, and matches the function with the expression parameter list in order according to the parameter defined by the function, removing unnecessary parameters, parameters that are not enough are processed by undefined, and then the function code is executed. Therefore, js overload functions must be implemented by determining the parameter values and types through function code.
When defining a function, you need to put the required parameters at the beginning of the parameter list, and the optional parameters are placed after the required parameters in the parameter list to facilitate function overloading.
How to Implement JS function Overloading
Javascript cannot support function overloading, as follows: copy the Code as follows: <script language = "JavaScript" function f (length) {alert ("high:" + length );} function f (length, width) {alert ("high:" + length + ", width:" + width) ;}</srcipt the above Code is actually not feasible, because the number of parameters for function definition does not have any relationship with the number of parameters for function calling. F. arguments [0] and f. arguments [1] gets the first and second parameters passed in when calling, so defining function (length) and calling f (10, 10) is no problem. So in the above Code, the second function can never be called. How can we implement functions like function overloading? In the function definition, f. arguments. length is used to determine the number of parameters passed in during the call. Then, different processing methods are used for different situations. The copy code is as follows: <script language = "JavaScript" function f () {var len = arguments. length; if (1 = len) {var length = arguments [0]; var width = arguments [1]; f2 (length, width );} else {var length = arguments [0]; f1 (length) ;}} function f1 (length) {alert ("high:" + length);} function f2 (length, width) {alert ("the height is:" + length + ", and the width is:" + width) ;}</srcept, you can give the function f () you can also input two parameters, for example, f (10) and f (10, 10). In my opinion, this method can be used to implement overload, but it is not very useful, we can implement overload in a function according to the actual situation. If the two functions to be overloaded have a large difference, we will keep the two functions. If the implementation of the two functions is similar, you can judge in a function and process different parts without writing three functions as shown in the preceding code: copy the Code as follows: <script language = "JavaScript" function f (length) {var len = arguments. length; alert ("height:" + length + ", width:" + width);} else {alert ("height:" + length) ;}</srcipt