OOP & Classes in JavaScript - Exercise 3

A static method belongs to the class itself rather than to individual instances.

javascript

class MathTools {
  static double(value) { return value * 2; }
}
console.log(MathTools.double(4));

Use the static keyword for data shared by the class rather than stored on each instance.

javascript

class User {
  static role = 'member';
}
console.log(User.role);

Composition combines focused capabilities without creating a rigid parent-child hierarchy.

javascript

const canLog = (object) => ({
  log(message) { console.log(message); return object; }
});
const service = canLog({ name: 'Orders' });

Keep state private and expose methods that validate every update.

javascript

class Progress {
  #value = 0;
  advance(step) {
    this.#value = Math.min(100, Math.max(0, this.#value + step));
  }
  get value() { return this.#value; }
}

It assigns an initial value directly in the class body before the constructor continues.

javascript

class Cart {
  items = [];
  currency = 'USD';
  add(item) { this.items.push(item); }
}

Use an arrow-function field or bind the method when passing it as a callback.

javascript

class Timer {
  seconds = 0;
  tick = () => { this.seconds += 1; };
}
setInterval(new Timer().tick, 1000);

Keep responsibilities focused, document the public contract, and avoid exposing mutable internal state.

javascript

class Repository {
  findById(id) { return this.records.find(record => record.id === id); }
  constructor(records = []) { this.records = [...records]; }
}

Use the simplest abstraction that fits. A plain object or function is often clearer when there is no shared lifecycle, identity, or behavior to model.

javascript

function formatPrice(amount, currency = 'USD') {
  return new Intl.NumberFormat('en', { style: 'currency', currency }).format(amount);
}

A class expression creates a class as part of an expression, making it useful when a class needs to be assigned or passed as a value.

javascript

const Counter = class {
  value = 0;
  increment() { this.value++; }
};
const counter = new Counter();

Validate constructor arguments and updates at the class boundary so instances cannot enter invalid states.

javascript

class Score {
  #value = 0;
  add(points) {
    if (points < 0) throw new Error('Points must be positive');
    this.#value += points;
  }
}
All sections expanded. Activate the control to collapse all sections.