Arrow Function
Is special way to handle functions with short callbacks
normal function:
function story(post, date){ return post + date;}
arrow function:
(post, date) => post + date;
Also, arrow function doesn’t shadow inner this (lexical this) so you don’t need to add another reference to this so you can use inside handler like below:
var posting = {
article: “Arrow Function”,
savePost: function(message, handler){ handler(message);
},
postArticle: function(){
var that = this;
this.savePost(“Article “, function(message){ that.article;//get article console.log(message + that.article); }); }
}
posting.postArticle();
Using ES6 arrow function:
var posting = {
article: “Arrow Function”,
savePost: function(message, handler){ handler(message);
},
postArticle: function(){ this.savePost(“Article “, message => console.log(message + this.article)); }
}
posting.postArticle();