1. There is no concept of overloading functions in javascript!
First of all, JavaScript does not have the concept of overloaded functions. A long time ago, when I used javascript to make web pages, I wrote some simple effects and did not need to use overloaded functions at all. When I was writing games, I wanted to use overloaded functions when there were a large number of functions. I didn’t expect that javascript would not support it.
Let's simply use two ways to "simulate" the overloaded function.
2.Judge according to the number of parameters
There is a variable called arguments in the JavaScript function, which is an array of parameters. We can use this to judge the number of parameters and then execute different contents separately. That is, the same function can have different effects, which is still very different from overloaded functions in strongly typed languages such as C++. You can write this way, comment out all the parameters and tell the user that this function supports up to 3 parameters, and the specific parameters will be obtained in the function. You must write more comments that support overloaded functions, so that they will be clearer. It is best to attach a call example.
/*** Return sum of a and b and less than limitNumber* @param {Number} a* @param {Number} b* @param {Number} 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.Judge according to the different parameter types
JavaScript has a keyword called typeof, which can determine the type of a variable.
var temp = "say"; //stringvar temp = 1; //numbervar temp = undefined; //undefinedvar temp = null; //objectvar temp = {}; //objectvar temp = []; //objectvar temp = true; //booleanvar temp = function (){} //function function testFunction(a){if(typeof(a) == "number"){//do something}else if(typeof(a) == "string"){//do something}}The above content is the relevant knowledge of JavaScript overloading functions introduced to you by the editor. Interested friends will learn together!