How do you override an object in JavaScript with the arguments object

  • 2020-03-30 02:54:32
  • OfStack

 
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Insert title here</title> 
<script type="text/javascript"> 
 
function add(a){ 
return a+10; 
} 
var add=new Function("a","return a+10"); 
//alert(add(5)); 
function add(num1,num2){ 
return num1+num2; 
} 
var add=new Function("num1","num2","return num1+num2"); 
alert(add(5,6)); 
alert(add(5));//The result of this call is NaN: a function with two arguments defined after the time of the call
//This means that even though there is a declaration of var, javascript will overwrite it as long as the variable name is the same
//The previous definition of ======= == concludes that there is no overloading of the function in js.

//-- overloading method with arguments object --
//- call different code blocks depending on the number of parameters, up to 25 parameters
function addNum(){ 
alert(arguments.length); 
for(var x=0;x<arguments.length;x++){ 
alert(arguments[x]); 
//This object can only love the function body
} 
if(arguments.length==1){ 
return arguments[0]+10; 
}else if(arguments.length==2){ 
return arguments[0]+arguments[1]; 
}else{ 
return " Parameter error, please check "; 
} 
} 
var value=addNum(10,20,30); 
alert(" Return value of the function: "+value);//The value of the result value is: "parameter error, please check"
//In fact, it is through the judgment of parameters, to achieve the call of different functions, and return different values; Doing so similarly implements overloading in Java
//But in essence, js is not overloaded, the same variable, in different places, if the value is assigned, will inevitably overwrite the previously declared variable. Of course,
//This excludes the relationship between the quantities inside the function and the variables outside the function.
</script> 
</head> 
<body> 

</body> 
</html> 

Related articles: