JavaScript Interview Question #30
What are object methods in JavaScript?
Objects, Arrays & Core Data Handling Junior Beginner
Quick Interview Answer
An object method is a function stored as an object property. Method shorthand allows the function keyword to be omitted inside an object literal.
Detailed Explanation
An object method is a function stored as an object property. Method shorthand allows the function keyword to be omitted inside an object literal.
Regular methods can access the receiving object through this when called as object.method(). The value of this is determined by the call site, so extracting a method and calling it separately can change its this value.
Code Example
const calculator = {
value: 10,
add(amount) {
this.value += amount;
return this.value;
}
};
console.log(calculator.add(5)); // 15