The concept of overloaded functions is not in 1.javascript!
First of all, JavaScript is not the concept of overloaded functions, a long time ago, I used JavaScript to do the Web page, write some simple effect, do not need to use overloaded functions, when writing games, there are a large number of functions, you want to use overloaded functions, I didn't think JavaScript didn't support it.
Let's simply "simulate" the overloaded function in two different ways.
2. According to the number of parameters to judge
JavaScript function has a variable called arguments, is a record parameter of an array, we can use this to determine the number of parameters, and then perform a different content, that is, the same function can have different effects, and C + + and other strong-type language overload function or a large difference. You can write this, the parameters are all commented out, tell the use of the function, this function supports up to 3 parameters, the specific parameters in the function to get. Annotations that support overloaded functions be sure to write more points, it will be clearer, preferably with a call example.
/**
* Return sum of A and B and less than Limitnumber
* @param {number} a
* @param {number} b
* @param {Nu Mber} limitnumber
/function Add (/*a, B, limitnumber*/) {
var a,b,limitnumber;
A = Arguments[0];
b = arguments[1];
if (arguments.length = = 3) {
limitnumber = arguments[2];
if (A + B > Limitnumber) {return
limitnumber;
}
}
return a + B;
}
3. According to the different types of parameters to determine
JavaScript has a keyword called typeof, which can be used to determine the type of a variable.
var temp = "Say"; String
var temp = 1;//number
var temp = undefined;//undefined
var temp = null;//object
var temp = {}; Object
var temp = [];//object
var temp = true;//boolean
var temp = function () {}//function
function TestFunction (a) {
if (typeof (a) = = "Number") {
//do something
}else if (typeof (a) = = "string") {
//do Something
}
}
The above content is small to introduce the JavaScript overload function of the relevant knowledge, interested friends to study together!