SoFunction
Updated on 2025-02-28

How to implement reloading and default parameters for js simulation

Simulate implementation overloading and default parameters

As we all know, js is a function that does not support overloading and default parameters, but we can use some other methods to simulate the implementation of this method.

  • First, let’s take a look at the definition of overload: the function name is the same, the function parameter list is different (including the number of parameters and parameter types), and the return type can be the same or different.
  • And the definition of default parameters: The default parameter refers to a value that is automatically used when the real parameter is omitted from the function call.

So how to implement these two functions? A very simple method is to use arguments to simulate.

Let's talk about the method of implementing overloading

	function overLoad(){//Simulate reloading using this method		if(arguments[0]){//If the first parameter exists			if(arguments[1]){//If the first parameter and the second parameter exist				//to do...
				alert(arguments[0]+arguments[1]);
			}
			else{//If only the first parameter				alert(arguments[0]);
				//to do...
			}
		}
		else{//If there is no parameter			alert("null");
			//to do...
		}
	}

Next is the way to implement the default parameters

function defaultArg(){//Simulate default parameters using this method    var a = arguments[0]?arguments[0]:"hello";//The default value of the first parameter is hello    var b = arguments[1]?arguments[1]:"world";//The default value of the second parameter is world    //...
    alert(a+b);
}

Let's do a test below

//Reload testoverLoad();//null
overLoad("hello ");//hello 
overLoad("hello ","world");//hello world
//Default parameter testdefaultArg();//hello world
defaultArg("Hello ");//Hello worlddefaultArg("Hello ","world");//Hello world

Does js function support overloading?

Does JavaScript function support overloading? There are two main points for this problem: first, JavaScript functions; second, overloading.

First, let’s talk about the overload. The so-called overloading, simply put, means that functions or methods have the same name but the parameter list is different. Such functions or methods with different parameters with the same name are called overloaded functions or methods. Therefore, overloading mainly requires two points: first, the same function name. Second, different function parameters.

After clarifying the definition of overload, let's go back to JavaScript. Tracing the origin, now when we talk about JavaScript, we can think of ECMAScript, that is, the standard of JavaScript. So, what specifications are made for functions in this standard?

First, ECMAScript has no concept of function signatures, because its parameters are represented by an array containing zero or multiple values. Without function signatures, real overloading is impossible.

Secondly, if two functions with the same name are defined in ECMAScript, the name only belongs to the function defined later, as follows:

function add(num){
    return num+1;
}
function add(num){
    return num+2;
}
var result = add(4);  //The result is6

In the above example, the add() function is defined twice. However, when we call it, we directly call the second function, which means that in JavaScript, the later defined function will override the first defined function.

Speaking of this, can we determine that JavaScript does not support function overloading?

Let me introduce an argument object in JavaScript. First of all, the parameters of ECMAScript functions are a little different from those of other languages. The ECMAScript function does not mind the number and type of parameters passed in. That is, after you define a function that accepts only two parameters, you can still pass zero or more parameters when invoking it. This will not report an error. The reason is the arguments object. In ECMAScript, the parameters of the function are always stored in an array, and the array can be accessed through the arguments object. Therefore, you only need to use the length attribute to determine how many parameters are passed when calling the function.

Speaking of this, we can try writing like this:

function add(num1, num2){
    if( == 1){
        alert("You only enter one number:"+arguments[0]+"Please re-enter");
    }else if( == 2){
        alert("You enter the sum of the numbers as:" + arguments[0]+arguments[1]);
}
}

Through this example, we can see that by checking the number of parameters in the passed function, the JavaScript function can react differently, which can indirectly achieve the purpose of overloading.

Therefore, JavaScript can mimic the overloading of functions.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.