Chapter XXI · The `this` Keyword
Object Method
When a function is called as a method of an object, `this` points to that parent owner object.
01 How It Works
Think of an employee pointing to their own department records workspace desk.
The object preceding the dot notation during method invocation becomes the binding value for `this`.
example.js
const user = {
name: "Bob",
getName() { return this.name; }
};
console.log(user.getName()); // "Bob"
1. Define a function property inside an object.
2. Invoke the method using dot notation (`object.method()`).
3. Reference object state via `this`.
02 Practical Example
Here is how you might see this concept applied in real-world code:
practical.js
const car = {
model: "Tesla",
showModel() { console.log(this.model); }
};
car.showModel();
Key Takeaway
Method invocation binds `this` to the object containing the method call.