🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Object Methods

AI & DATA SCIENCE // methods

Methods are functions stored as object properties. They access the object via 'this'.

Syntax

const obj = {
  name: 'Alice',
  greet() { // shorthand method
    return `Hello, ${this.name}`;
  }
};

Deep Dive Course

A **method** is a function that is a property of an object. It typically uses `this` to refer to the object it belongs to. The **shorthand method** syntax (`greet() {}`) is preferred over assigning function expressions. Arrow functions as methods are problematic because they don't bind `this`.

1Understanding Object Methods

A method is a function that is a property of an object. It typically uses this to refer to the object it belongs to. The shorthand method syntax (greet() {}) is preferred over assigning function expressions. Arrow functions as methods are problematic because they don't bind this.

💡

Never use arrow functions as object methods when you need 'this' to refer to the object.

editor.html
const counter = {
  count: 0,
  increment() { this.count++; },
  decrement() { this.count--; },
  reset()     { this.count = 0; },
  getValue()  { return this.count; }
};

counter.increment();
counter.increment();
counter.increment();
counter.decrement();
console.log(counter.getValue()); // 2
localhost:3000

2Practical Example

Here is a real-world application of Object Methods showing how it is used in production JavaScript code.

editor.html
// 'this' problem with arrow functions
const obj = {
  name: 'Alice',
  greetGood() { return `Hello, ${this.name}`; }, // OK
  greetBad: () => `Hello, ${this.name}`,           // undefined!
};
console.log(obj.greetGood()); // Hello, Alice
console.log(obj.greetBad());  // Hello, undefined
localhost:3000

3Best Practices

Follow these guidelines when working with Object Methods:

1. Use shorthand method syntax

2. Never use arrow functions as methods needing 'this'

3. Extract complex logic to standalone functions and call them from methods

⚠️

Tip: Never use arrow functions as object methods when you need 'this' to refer to the object.

editor.html
const counter = {
  count: 0,
  increment() { this.count++; },
  decrement() { this.count--; },
  reset()     { this.count = 0; },
  getValue()  { return this.count; }
};

counter.increment();
counter.increment();
counter.increment();
counter.decrement();
console.log(counter.getValue()); // 2
localhost:3000

Examples

Example 01Basic Usage
const counter = {
  count: 0,
  increment() { this.count++; },
  decrement() { this.count--; },
  reset()     { this.count = 0; },
  getValue()  { return this.count; }
};

counter.increment();
counter.increment();
counter.increment();
counter.decrement();
console.log(counter.getValue()); // 2
Example 02Advanced Example
// 'this' problem with arrow functions
const obj = {
  name: 'Alice',
  greetGood() { return `Hello, ${this.name}`; }, // OK
  greetBad: () => `Hello, ${this.name}`,           // undefined!
};
console.log(obj.greetGood()); // Hello, Alice
console.log(obj.greetBad());  // Hello, undefined

Best Practices

  • Use shorthand method syntax
  • Never use arrow functions as methods needing 'this'
  • Extract complex logic to standalone functions and call them from methods

Interview Question

Why does 'this' sometimes refer to undefined or the window?

Hint: This depends on how the function is CALLED, not where it's defined.

In strict mode, 'this' inside a regular function is undefined if called without a receiver. In non-strict mode, it defaults to the global object (window). Arrow functions don't have their own 'this' — they inherit it lexically. call(), apply(), and bind() can explicitly set 'this'.

Exercises

MediumPractice using Object Methods in a real scenario.
View Solution
const counter = {
  count: 0,
  increment() { this.count++; },
  decrement() { this.count--; },
  reset()     { this.count = 0; },
  getValue()  { return this.count; }
};

counter.increment();
counter.increment();
counter.increment();
counter.decrement();
console.log(counter.getValue()); // 2

Frequently Asked Questions

Why does 'this' sometimes refer to undefined or the window?

In strict mode, 'this' inside a regular function is undefined if called without a receiver. In non-strict mode, it defaults to the global object (window). Arrow functions don't have their own 'this' — they inherit it lexically. call(), apply(), and bind() can explicitly set 'this'.

Related Functions

thisObject-CreatingPropertiesClasses