czwartek, 3 września 2015

ES6 - arrows

The biggest difference between arrow and function is this value. Function uses its own this value, but arrow uses this value of the enclosing context.

Arrows are always anonymous functions!

Their syntax is slightly different than function expression:
(args) => { statements }


There could be few variations from this main version:
(arg1, arg2, argN) => { statements }
arg1 => { statements }
() => { statements }

(arg1, arg2, argN) => { statements }
(arg1, arg2, argN) => expression
(arg1, arg2, argN) => ({ prop: val })


It means - prentheses are optional. Mostly if there is only one argument, or only one value to return, this could be simplified.
But there is a catch:
(arg1, arg2, argN) => expression

but
(arg1, arg2, argN) => { return expression }


Example:
(arg1, arg2) => arg1 * arg2

(arg1, arg2) => { return arg1 * arg2 }


Both return the same, but we can write it in different way.

Brak komentarzy:

Prześlij komentarz