I am little new to ES6, but not with node js, but this looks little weird because when I started using arrow style function instead of function() style, I was in assumption that both are exactly same but the syntax is different. But today came to know about this case.
When I define a function like this.
exports.intent = {
greetings: {
matcher: ["hi", "hello"],
reply: function(){
console.log(this); // it prints {greetings: {..}} expected
return this.matcher[0]; // returns "hi" which is expected.
}
}
}
But when I defined same function in arrow style, it throws error as this here now references the whole object instead of current object.
exports.intent = {
greetings: {
matcher: ["hi", "hello"],
reply: () => {
console.log(this); // it prints whole object {intent: {....}}
return this.matcher[0]; // fail here.
}
}
}
I came to know that there are some changes in this refrencing between these 2 types of declaration. https://stackoverflow.com/a/40498293/2754029
Is there are any way so that I can refrence this to current object only?