Abhishek Yadav
Apr-2018
// Traditional function -
var foo = function (x) {
return x*2;
}
// Equivalent arrow function
var foo = x => x * 2;
Syntax
// Traditional function -
var foo = function (x) {
return x*2;
}
// Equivalent arrow functions
var foo1 = (x) => { return x * 2 };
var foo2 = (x) => x * 2;
var foo3 = x => x * 2;
Goals
Non binding of this
Non binding of this
// Traditional function -
var obj = {
x: 99,
method1: function(){
console.log(this.x); // 99
setTimeout(function(){ // *this* changes here
console.log(this.x); // undefined
}, 1000);
}
}
Non binding of this - using arrow function
// 'that' hack -
var obj = {
x: 99,
method1: function(){
console.log(this.x); // 99
setTimeout(() => {
console.log(that.x); // 99
}, 1000);
}
}
Non binding of this - using arrow function
Terse code
list = [
{ type: 'x', score: 99 },
{ type: 'x', score: 49 },
{ type: 'y', score: 88 },
{ type: 'y', score: 57 }
]
// totalScoreTypeX =
list
.filter(function(item){
return item.type === 'x';
})
.map(function(item){
return item.score;
})
.reduce(function(acc, score){
return acc + score;
})
list = [
{ type: 'x', score: 99 },
{ type: 'x', score: 49 },
{ type: 'y', score: 88 },
{ type: 'y', score: 57 }
]
// totalScoreTypeX =
list
.filter(item => item.type === 'x' )
.map(item => item.score )
.reduce((acc, score) => acc + score)
Restrictions
// 1. Can't have name
// 2. Doesn't have arguments object
let foo = () => arguments // Error
Restrictions
// 3. Cant be used as constructor
const MyClass = () => {}
let obj = new MyClass() // Error
Restrictions
// 4. Doesn't have prototype property
let foo = () => {}
foo.prototype // undefined
Restrictions
// 5. Cant be used as generator function
let genFoo = () => {
yield 123 // Error
}
Restrictions
// 6. Can't be used as object method
let obj = {
x: 99,
method1: () => {
console.log(this.x); // undefined. Not 99
}
}
Restrictions (more)
Caveats
// Returning object literals in
// concise syntax (one liners) needs caution
let foo = () => { a: 1 } // Bug. Doesn't return object.
// Doesn't fail
// Assigns a label actually
let foo = () => ({ a: 1 }) // function returns object
Caveats
// Precedence is not same as a traditional function
let func;
func = func || function(){} // ok
func = func || () => {} // syntax error
// precedence rules problem
func = func || (() => {}) // ok
Browser support
Trivia - Other arrows in JS
<-- // Comment
--> // Comment and 'goes-to'
<= // The less-or-equal-to operator
Trivia - other arrows <--
<script language="javascript">
<!--
console.log(document.body)
// -->
</script>
// <-- is a single line comment in JS
// The above will be treated as
// HTML comment in a JS-disabled browser
// Otherwise, its valid Javascript
// surrounded by comment lines
// This too was standardized in ES6
Trivia - other arrows -->
// --> is also a single line comment in JS
// Everything after the arrow is treated as
// a comment
// And in HTML,
// everything *before* is a comment
// Also,
// it should appear at the beginning of a line.
Trivia - other arrows -->
// --> is also works as the "goes-to" operator
while (n --> 0) {
console.log(n);
}
// n goes-to 0
// Is actually just decrement and compare
// and not a special operator
(n--) > 0
(n=n-1) > 0
References