Runtime 36 ms Beats 99.23% & Memory 48.68 MB Beats 75.58%
ApproachMethod chaining in JavaScript classes allows you to call multiple methods on the same instance in a single statement. This is typically achieved by havi

# Approach
Method chaining in JavaScript classes allows you to call multiple methods on the same instance in a single statement. This is typically achieved by having each method return the instance (this) so you can chain further method calls.

Return this: Each method returns this, which refers to the current instance of the class. This allows subsequent method calls to operate on the same instance.

Avoid Breaking the Chain: If a method returns something other than this (e.g., a primitive value), the chain will break. Make sure only methods explicitly designed to end the chain return non-instance values.

Immutability (Optional): For functional programming enthusiasts, you can return a new instance instead of modifying the original one to preserve immutability. But for simplicity, modifying the instance works well for most cases.

# Code
```javascript []
class Calculator {
  /**
   * @param {number} value
   */
  constructor(value = 0) {
    this.result = value;
  }

  /**
   * @param {number} value
   * @return {Calculator}
   */
  add(value) {
    return new Calculator(this.result + value);
  }

  /**
   * @param {number} value
   * @return {Calculator}
   */
  subtract(value) {
    return new Calculator(this.result - value);
  }

  /**
   * @param {number} value
   * @return {Calculator}
   */
  multiply(value) {
    return new Calculator(this.result * value);
  }

  /**
   * @param {number} value
   * @return {Calculator}
   */
  divide(value) {
    if (value !== 0) {
      return new Calculator(this.result / value);
    } else {
      throw new Error("Division by zero is not allowed");
    }
  }

  /**
   * @param {number} value
   * @return {Calculator}
   */
  power(value) {
    return new Calculator(this.result ** value);
  }

  /**
   * @return {number}
   */
  getResult() {
    return this.result;
  }
}
```
['JavaScript']