Chapter VII · Objects
Methods
Methods are functions stored as object properties that define behaviors associated with the object.
01 How It Works
Think of a smart device equipped with built-in control action buttons.
Inside a method, the this keyword provides reference access to the owner object context.
example.js
const user = {
name: "Bob",
sayHello() {
console.log(`Hi, I am ${this.name}`);
}
};
user.sayHello();
1. Define a function property inside an object literal.
2. Use the this keyword to access peer object properties.
3. Invoke via dot notation method call.
02 Practical Example
Here is how you might see this concept applied in real-world code:
practical.js
const counter = {
count: 0,
increment() { this.count++; }
};
counter.increment();
Key Takeaway
Methods bundle behavior directly with object state data.