Lesson 17 / 38
Object Methods & this
Functions as properties, and what `this` points to.
Methods
A function stored as a property is a method — call it with object.method().
const dog = {
name: "Rex",
bark() {
return `${this.name} says Woof!`;
},
};
console.log(dog.bark()); // Rex says Woof!What this refers to
Inside a regular method, this refers to the object the method was called on — not where it was defined.
Losing this
Passing dog.bark as a standalone callback loses its this binding — use .bind() or an arrow wrapper to fix it.