JavaScript Interview Question #31
What does the this keyword mean inside an object method?
Objects, Arrays & Core Data Handling Junior Beginner
Quick Interview Answer
For a regular function invoked as obj.method(), this normally refers to obj. However, this is not permanently attached to the function; its value depends on how the function is called.
Detailed Explanation
For a regular function invoked as obj.method(), this normally refers to obj. However, this is not permanently attached to the function; its value depends on how the function is called.
Arrow functions do not create their own this, so they are usually not appropriate as object methods when the method needs a dynamic receiver.
Code Example
const user = {
name: "Asif",
showName() {
console.log(this.name);
}
};
user.showName(); // Asif
const show = user.showName;
// show(); // this is no longer user