Functions in ECMAScript are declared by using the function keyword followed by a set of parameters and the body of the function. The basic syntax is as follows,
1 {2 function functionname (arg0, arg1,..., argN) {3 statement4 }5 }
The function declaration must be called, the declared function can be called by its function name, followed by a bunch of parentheses and parameters (if there are more than one parameter in parentheses, can be separated by good).
1 {2 // function Declaration 3 function sayhi (name, message) {4 console.log (' Hello ' + name + ', ' + message); 5 }6 // function call 7 sayhi (' Lili ', ' Good job! ' ); 8 }
The function in ECMAScript does not have to specify a time to return a value when it is defined, and the function can implement the return value at any time by the return statement followed by the value to be returned. Also, the function stops and exits immediately after executing the return statement, and any code after the return statement is never executed.
1 {2 function sum (NUM1, num2) {3 return NUM1 + num2; 4 alert (); // Will never execute 5 }6 }
The return statement can also have no return value. In this case, the function returns a undefined value after it stops executing. This usage is typically used in situations where you need to stop the function function execution prematurely without requiring a return value.
Understanding Parameters
ECMAScript's function does not mind passing in the number of functions, and does not care about what data type to pass in the parameter. Even if you define a function that accepts only two parameters, it is not necessarily necessary to pass two when calling a function. You can pass one, three, or even pass parameters, and the parser will never have any complaints. The reason for this is that the parameters in ECMAScript are internally represented by an array. This array is always accepted by the function, regardless of which parameters are included in the array. If the array does not contain any elements, it does not matter. If you include more than one element, there is no problem. In fact, the parameter array can be accessed through the arguments object in the body of the function to obtain each parameter passed to the function.
In fact, the arguments object is just like an array, so you can access each of its elements using the square bracket syntax, using the Length property to determine how many arguments are passed in.
Another important feature is that named parameters are only convenient, but not required. In addition, in terms of named parameters, other languages may need to create a function signature beforehand, and future calls must be consistent with that signature. However, ECMAScript is different, the ECMAScript parser does not validate that the named parameter is consistent with the parameter declaration.
1 {2 functionsum () {3 if(arguments.length!== 0) {4 varLength =arguments.length;5 vars = 0;6 for(vari = 0; i < length; i++) {7s + =Arguments[i];8 }9 returns;Ten } One return0; A } -SUM (1, 2);//3 -SUM ();//0 theSUM (1, 2, 3, 4, 5);// the -}
No overloads
The ECMAScript function cannot be overloaded as it is traditionally meant, because the function of ECMAScript is not signed, and its arguments are represented by an array of 0 or more values. Without a function signature, the real overload is impossible.
Since there is no overloaded feature, if two functions with the same name are defined in ECMAScript, the name belongs only to the post-defined function.
1 {2 function sum () {3 console.log (1); 4 }5 function sum () {6 console.log (2); 7 }8 sum (); 29 }
JavaScript Chapter-----Functions