Concept
Unlike Regular function , arrow function
- do not have their own this value,
- this always inherit from enclosing scope
let message = {
name : 'John',
regularFunction: function(){
console.log(this);
return 'Hello,' + this.name;
},
arrowFunction: () =>{
console.log(this);
return 'Hello, ' + this.name
}
}
console.log(message.name);
console.log('-----------');
console.log(message.regularFunction());
console.log('-------');
console.log(message.arrowFunction());
Execution Output
(base) C:\Users\inaka\Documents\coding\npm_test>node arrow_regular_vs.js
John
-----------
{
name: 'John',
regularFunction: [Function: regularFunction],
arrowFunction: [Function: arrowFunction]
}
Hello,John
-------
{}