this inside an object method
Short answer
Inside an object method, the value of this points to the object itself through which the method was called.
Example
const user = {
name: 'Oleh',
sayHi() {
console.log(this.name);
}
};
user.sayHi(); // "Oleh"Here:
thisinsidesayHi()points touser,- because the method is called as
user.sayHi().
How it works under the hood
When the call:
obj.method()is executed, JS does two things:
- Looks up
methodinsideobj(or its prototypes), - Calls it with a bound context -
this = objat call time.
If you save the method to a variable - the context is lost
const user = {
name: 'Oleh',
sayHi() {
console.log(this.name);
}
};
const fn = user.sayHi;
fn(); // undefined (or an error in strict mode)Why?
- With
fn()the method is called without an object. - So
thisinside the function no longer points to user:- in strict mode ->
this = undefined; - in non-strict mode ->
this = window(in the browser).
- in strict mode ->
How to restore the correct context
Method 1 - call through the object
user.sayHi(); // worksMethod 2 - use bind()
const fn = user.sayHi.bind(user);
fn(); // "Oleh"bind() creates a new function with a permanently "bound" context.
Example with nested objects
const user = {
name: 'Oleh',
address: {
city: 'Kyiv',
getCity() {
console.log(this.city);
}
}
};
user.address.getCity(); // "Kyiv"Here this points to address,
because the method was called as user.address.getCity().
But if you take the function out...
const cityFunc = user.address.getCity;
cityFunc(); // undefinedThe context is lost - the call happens without an object.
this does not depend on where the function is declared
Only on how it is called:
function show() {
console.log(this);
}
const obj1 = { show };
const obj2 = { show };
obj1.show(); // this = obj1
obj2.show(); // this = obj2The same function code,
but this is different - because it was called through different objects.
In arrow functions this behaves differently
const user = {
name: 'Oleh',
sayHi: () => {
console.log(this.name);
}
};
user.sayHi(); // undefinedArrow functions have no own this -
they take this from the outer lexical environment (where they were declared).
In this case - from the global scope.
That is why arrow functions should not be used as object methods.
Example where an arrow function is actually appropriate
Arrow functions are useful when you need to preserve this from the outer method:
const user = {
name: 'Oleh',
friends: ['Bob', 'Max'],
showFriends() {
this.friends.forEach(friend => {
console.log(`${this.name} knows ${friend}`);
});
}
};
user.showFriends();
// "Oleh knows Bob"
// "Oleh knows Max"Here friend => does not create a new this,
so the arrow function uses this from the showFriends() method.
SUMMARY
| Situation | What this is |
|---|---|
obj.method() | obj itself |
method() without an object | undefined (or window in old mode) |
| Arrow function | this from the outer scope |
bind(obj) | always obj, regardless of the call |
call / apply | set this manually at call time |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.