const profile = {
name: 'Alex',
getName :() =>{
return this.name;
}
};
Here I am not getting the name as 'Alex'. But when I am using the function keyword instead, I am getting the desire result. Why?
const profile = {
name: 'Alex',
getName :() =>{
return this.name;
}
};
Here I am not getting the name as 'Alex'. But when I am using the function keyword instead, I am getting the desire result. Why?
this
with Arrow Functions:
Significant advantage it offers is the fact that it does not bind its own this. In other words, the context inside arrow functions is lexically or statically defined.
if you are accessing method inside object you need to use regular function instead of arrow function.
const profile = {
name: 'Alex',
getName :function(){
return this.name;
}
};
For more clarification of this
, you can visit this keyword