// method of defining a function 1 function ABS (x) { if (x >= 0) { return x; Else { return -x; }}
abs()
the above functions are defined as follows:
function
Indicates that this is a function definition;
abs
is the name of the function;
(x)
The parameters of the function are listed in parentheses, and multiple parameters are ,
separated;
{ ... }
The code between is a function body, can contain several statements, and even can not have any statements.
Note that when the statement inside the function body executes, the function executes and returns the result as soon as it is executed return
. Therefore, it is possible to implement very complex logic within a function through conditional judgments and loops.
If there is no return
statement, the result is returned after the function is executed, except for the result undefined
.
Since the function of JavaScript is also an object, the abs()
function defined above is actually a function object, and the functions name abs
can be considered as a variable pointing to the function.
// method of defining a function 2 var function (x) { if (x >= 0) { return x; Else { return -x; }};
In this way, function (x) { ... }
it is an anonymous function that does not have a function name. However, this anonymous function assigns a value to the variable abs
, so abs
the function can be called by a variable.
The two definitions are completely equivalent , and note that the second way is to add one at the end of the function body according to the complete syntax ;
, indicating the end of the assignment statement.
Calling functions
When you call a function, you pass in the arguments sequentially:
Because JavaScript allows you to pass in any parameter without affecting the call, there is no problem with the parameters passed in that are more than the defined parameters, although they are not required inside the function:
There is no problem with the parameters passed in that are less than defined:
abs(x)
the parameters of the function are received at this time x
undefined
, and the computed result is NaN
.
To avoid receipt undefined
, you can check the parameters:
function ABS (x) { if (typeof x!== ' number ') { throw ' not a number ' c13>; } if (x >= 0) { return x; Else { return -x; }}
Arguments
JavaScript also has a free keyword arguments
that works only inside the function and always points to all parameters passed in by the caller of the current function. arguments
similar Array
but it is not a Array
:
function foo (x) { // tenfor (var i=0; i<arguments.length; i++ ) { /// , }}foo (10, 20, 30);
Using arguments
, you can get all the parameters that the caller passed in. That is, even if the function does not define any parameters, it can get the value of the parameter:
function abs () { if (arguments.length = = = 0) { return 0; } var x = arguments[0]; return x >= 0? X:-/ /0// // 9
In fact arguments
, it is most commonly used to determine the number of incoming parameters. You may see the following notation:
// foo (a[, b], c) // receives two or three parameters, B is an optional parameter, and if only 2 parameters are passed, B defaults to null: function Foo (A, B, c) { if (arguments.length = = = 2) { // The actual parameters obtained are A and B, C for undefined // assign B to c null// B to default value } // ...}
To change the intermediate parameter to b
an "optional" parameter, you can only pass the arguments
judgment and then re-adjust the parameter and assign a value.
Rest parameters
Since JavaScript functions allow the receipt of arbitrary parameters, we have to use arguments
them to get all the parameters:
function Foo (A, b) { var i, rest = []; if (Arguments.length > 2) { for (i = 2; i<arguments.length; i++) { Rest.push ( Arguments[i]); } } Console.log (' a = ' + a); Console.log (' b = ' + b); Console.log (rest);}
In order to obtain a
b
parameters other than the defined parameters, we have to use arguments
, and the loop to start from the index 2
to exclude the first two parameters, this is very awkward, just to obtain additional rest
parameters, there is no better way?
The ES6 standard introduces the rest parameter, the function above can be rewritten as:
functionFoo (A, B, ... rest) {Console.log (' A = ' +a); Console.log (' B = ' +b); Console.log (rest);} Foo (1, 2, 3, 4, 5);//Results://A = 1//B = 2//Array [3, 4, 5]Foo (1);//Results://A = 1//B = Undefined//Array []
The rest parameter can only be written at the end, preceded by the ...
identification, from the running results, we know that the passed parameter is bound first, a
b
and the extra parameters are given to the variable in the array form, rest
so we don't need to arguments
get all the parameters.
It does not matter if the passed parameters are not filled with the normal defined parameters, and the rest parameter receives an empty array (note not undefined
).
05.javascript--entry function