Js function overloading of

Function overloading and js

What is the function overloading

Overloads function is a special case, for ease of use, C ++ allows the declaration of a function similar to the same name in the same range of several functions, but these functions of the same name in the form of parameters (parameter refers to the number, type or order) must be different , that perform different functions with the same function. This is overloaded functions. Overloaded function used to implement the functions and the like are different types of data processing problems. Not only function return values of different types.

Similarly rewriting function

Rewriting function, also called covered, it refers to the subclass redefine the parent class virtual function with the same name and parameters, mainly in the inheritance relationship.

The basic function overloading conditions

  • Function name must be the same;
  • Function parameters must not be the same, may be a different number of parameters or parameter type;
  • Function returns the value may be the same, may not be the same. (If the name and function parameters are identical, only return values ​​of different types, a function is not overloaded.)

Function overloading scenarios

Reloaded at the same scene, the same function for the function, only the different parameters, can reduce the development of the repeated names, etc.

Function overloading in javascript

javascript function overloading is not in the true sense, because of the same name in the javascript function in the same scope, the former will be covered by the latter, but the same effect can be achieved indirectly overridden by other methods, the javascript function not signed, it parameter is an array containing a plurality of zeros represented. No function signature, then overloading is impossible

But we can achieve the introduction method overloading effect, use the arguments object is an array of class objects inside functions, which it holds the function is called, all the parameters passed to the function. Simply speaking is to use logic, to execute different code depending on the length of the array where the parameters

function foo(){
    if(arguments.length === "条件1"){
        "函数1"
    }
    if(arguments.length === "条件2"){
        "函数2"
    }
}

Overload is the essence of the function of many similar functions merged into a single function

Guess you like

Origin www.cnblogs.com/baiyang2292/p/11713653.html