Hello,
Traditional javaScript function has the Method, Non-methods and constructor and have their own this
keyword, so it causes the this
binding. To overcome this problem coffeeScript provides an Arrow function, Called "javaScript arrow" Function or " fat Arrow " function. It is an anonymous function and can not be used as constructor. Its behaviours is different as compare to the traditional javaScript behavior in the following manner:-
- Lexical
this
binding.
- Not
new
able.
- Can not change
this
.
It has the following characteristic of the arrow function:-
- JavaScript arrow function are anonymous function.
- Arrow function can not be used as a constructor function.
- Lexical binding of
this
inside arrow function that means this
keyword always point out the same function throughout the arrow function.
The Syntax:- Here is some example of the syntax of the arrow function.
- No Parameter:-
/no params, empty body
() => {};
//equivalent to
function(){}
- With single Parameter:-
//single param, single statement
x => x;
//traditional function with single parameter
function(x){
return x;
}
- With multiple params:-
//multiple params
(x, y) => x * y;
//traditional function with multiple params
function(x, y){
return x * y;
}
- Multiple params and statements
:-
(x, y) => {
var z = 5;
return (x * y) + z;
}
- Wrap function BODY in parenthesis:-
x => ({id: x, height: 270});
The javascript arrow function can be remember by following rules:-
- We can omit the () for the single parameter arrow function.
- We can wrap the parameters in the () for the multiple parameters or no parameters arrow function.
The lexical this
binding can solve the many problem as compare to the traditional javascript function for the developer. It improves the javascript engine optimization as compare to the traditional javascript function. It concise the syntax also.
0 Comment(s)