Call object methods with a receiver
Quick answer
A method is callable behavior associated with an object property. For an ordinary method call, the expression before the dot supplies the receiver used as this.
How it works
Use { price: 5, total(q) { return this.price * q; } }. Calling item.total(3) supplies item as the receiver. The same function can be stored on another object and called with that object as receiver.
const item = { price: 5, total(q) { return this.price * q; } };console.log(item.total(3));Expected output in Node.js:
15this.price reads the receiver property.
const a = { price: 4, total(q) { return this.price * q; } };const b = { price: 9, total: a.total };console.log(a.total(2), b.total(2));Expected output in Node.js:
8 18The same callable value sees different receivers.
Common mistakes
Extracting a method into a variable does not preserve its receiver. Arrow functions do not create their own this; use an ordinary method when a dynamic receiver is needed.
Practice
Create two objects with different unit prices and reuse one total method.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Call object methods with a receiver — Knowledge Check
Apply the taught concept and reason about the result.
In an ordinary call item.total(3), what supplies this?
For const a={price:5,total(q){return this.price*q;}} what is a.total(4)?
If the same ordinary method is assigned to a second object and called there, what can change?
Does extracting const f=item.total preserve the receiver automatically?
Why is an arrow not a direct replacement for a dynamic-receiver method?