Defining an arrow function is simple, and the basic syntax is:
([param] [, param]) = {
Statements
}
param = Expression
Param is a parameter, depending on the number of parameters, the following conditions:
- () = {...}//0 parameters expressed in ();
- x = {...}//a parameter can be omitted ();
- (x, y) = {...}//Multiple parameters cannot be omitted ();
Of course, as with normal functions, the arrow functions can also use ES6 's new "Default parameters" and "Remaining parameters" (firefox15+ start support):
var func1 = (x = 1, y = 2) = = x + y;
Func1 (); Get 3
var Func2 = (x, ... args) = = {Console.log (args)};
Func2 (a); Output [2, 3]
The arrow function allows multiple lines of statements or single-line expressions to function as bodies. A multiline statement is enclosed in {}, a single-line expression does not require {}, and it returns a value as a function:
x = {return x * x}; function returns x * x
x = x * x; Previous line
x = return x * x; SyntaxError error, cannot omit {}
x = {x * x}; Legal, no return value defined, return undefined
The arrow functions are also one of the JS functions, so the previous instanceof and TypeOf are still available:
var func1 = () = {};
Func1 instanceof Function; True
var Func2 = () = {};
typeof Func2; "Function"
JS = function