Sub topic 4
Method Calls and the Object Before the Dot
This chapter assumes you know how to write object literals and methods, and that you have read the previous chapter on plain function calls. In this chapter, "receiver" means the object that a method is called on.
The Rule: Look Left of the Call
When you call a function as a property of an object, JavaScript sets this to that object. This is called implicit binding. To apply it, find the call parentheses and look at the expression just before them. If that expression reads a property, written obj.prop or obj[key], then obj becomes this for that call.
const user = {
name: 'Grace',
describe() {
return `I am ${this.name}`;
}
};
console.log(user.describe()); // 'I am Grace'
console.log(user['describe']()); // 'I am Grace', bracket access works tooImplicit binding treats bracket notation the same as dot notation. What matters is that a property is read and then called in a single expression.
The Function Does Not Belong to the Object
An object does not own its methods. A property only holds a reference to a function object, and several objects can hold the same function. Every call works out this again from scratch.
function describe() {
return `I am ${this.name}`;
}
const alan = { name: 'Alan', describe };
const ada = { name: 'Ada', describe };
console.log(alan.describe()); // 'I am Alan'
console.log(ada.describe()); // 'I am Ada'
console.log(alan.describe === ada.describe); // true, one functionThe same function returns different results because the receiver changes between calls. Where the function was written plays no part.
Only the Last Object Counts
In a chain of property accesses, this is only the object directly before the final property. The objects further to the left are not involved.
const company = {
name: 'Acme',
ceo: {
name: 'Rosa',
introduce() {
return `${this.name} speaking`;
}
}
};
console.log(company.ceo.introduce()); // 'Rosa speaking', this is company.ceoA common mistake is to expect this to be the outermost object, company. JavaScript evaluates company.ceo first, which produces an object, and then calls introduce on that object.
Methods Inherited Through Prototypes
If an object inherits a method through its prototype chain, this is still the object you called the method on. It is not the prototype that stores the method.
const animal = {
speak() {
return `${this.sound}!`;
}
};
const dog = Object.create(animal);
dog.sound = 'Woof';
console.log(dog.speak()); // 'Woof!', this is dog, not animalPrototype-based sharing depends on this behavior. A single method stored on the prototype can serve every instance, because each call gets the instance as this.
Syntax That Keeps the Receiver
Some syntax variations still count as method calls:
| Expression | Is `this` the object? | Reason |
|---|---|---|
| `obj.m()` | Yes | Standard method call |
| `obj['m']()` | Yes | Bracket access is still a property access |
| `obj?.m()` | Yes | Optional chaining keeps the receiver |
| `(obj.m)()` | Yes | Grouping parentheses do not change the reference |
| `(0, obj.m)()` | No | The comma operator returns a bare function value |
The last row shows where implicit binding stops. When an expression reduces the method to just its function value, the link to obj is gone. The next chapter covers this case in detail.
Common Errors
- Expecting the outer object in a chain. In
a.b.c(),thisisa.b. - Expecting the prototype instead of the instance. An inherited method gets the instance it was called on.
- Thinking the definition site matters. A method written inside
usergets whatever receiver the call provides.
Exercise
Given const shop = { name: 'Shop', cart: { name: 'Cart', label() { return this.name; } } };, what does shop.cart.label() return, and why?
Show answer
It returns 'Cart'. The object directly to the left of the final property label is shop.cart, so this is the cart object. The outer shop object has no effect on the call.
Exercise
const base = { who() { return this.id; } }; const item = Object.create(base); item.id = 7; What does item.who() return, and what would base.who() return?
Show answer
item.who() returns 7 because this is the receiver item, even though who is stored on base. base.who() returns undefined because base has no id property.