1, JS in the function of the overloaded: JS function, parameters are not required, a function hypothesis defines a few parameters, then the caller can pass, or can not pass, can also be only a two .... can also be transmitted a lot, so we can not rely on the number of parameters to distinguish between two functions of the same name (the code below understanding)
<! DOCTYPE html>functionFA (A, B) {alert (a+b); } functionfa () {alert (10); } FA (300,600); varFB =function(A, B) {Alert (A +b)}; FB=function() {Alert (10);}; FB (12, 16);//Ten varFC =NewFunction (A, B, "alert (a+b)")); FC=NewFunction ("alert (10)")); //js function, parameters are not required, a function hypothesis defines a few parameters, then the caller can pass, or can not pass, can also be only a two .... Can also be passed many, so we can not rely on the number of parameters to distinguish between two functions of the same name //The overloaded simulation of functions in JS: functionDoadd () {//arguments object: An array of parameters. if(Arguments.length = = 1) {alert (arguments[0] + 10); } Else if(Arguments.length = = 2) {alert (arguments[0] + arguments[1]); } } //a function is called, and two kinds of results are obtained by the number of arguments passed. Doadd (10);// -Doadd (30, 20);// -</script>
2, JS in the function of the parameter array (arguments) access
<! DOCTYPE html>//arguments parameter array functionSayhi () {alert ("Hello" + arguments[0] + "," + arguments[1]);//access to parameter arrays} sayhi ("Nicholas", "How is you today?"); functionHowmanyargs () {alert (arguments.length);//length of the parameter array} howmanyargs ("ing", 45);//2Howmanyargs ();//0Howmanyargs (12);//1</script>Functions in JavaScript