1

I am try to make a chainable instance for a personal project, and I try this simple example with some modifications, but I got a error:

TypeError: chainableInstance.firstMethod(...).secondMethod is not a function
    at Object.<anonymous> (/Users/rodger/Developer/Projects/Personal/unknown/src/teste.js:48:33)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

And my code is this:

  otherMethod(a) {
    return (this.a = a.replace('!e', 'e'));
  }
}
class Another extends Other {
  constructor() {
    super();
  }

  anotherMethod(a) {
    console.log('This is anohter method');
    return (this.a = a.replace('!d', 'd !e'));
  }

  otherMethod() {
    return super.otherMethod(this.a);
  }
}

class ChainAble extends Another {
  constructor() {
    super();
  }

  firstMethod() {
    return (this.a = 'a !b');
  }

  secondMethod() {
    return this.a.replace('!b', 'b !c');
  }

  thirdMethod() {
    return this.a.replace('!c', 'c !d');
  }
  anotherMethod() {
    return super.anotherMethod(this.a);
  }
}

const chainableInstance = new ChainAble();
chainableInstance.firstMethod().secondMethod().thirdMethod().anotherMethod();

console.log(chainableInstance);

I just not undestand why my secondMethod is It is considered "not a function", have someone to see what I am wrong in this stuff?

nikan
  • 21
  • 4
  • 2
    Calling some class `Chainable` doesn't make it chainable. You actually need to return an instance from the first method for that! None of your methods even returns an object. – Bergi May 30 '20 at 16:25
  • Does this answer your question? [Method Chaining in a Javascript Class](https://stackoverflow.com/questions/48219415/method-chaining-in-a-javascript-class) – Ivar May 30 '20 at 16:26

2 Answers2

1

It's because firstMethod returns the string 'a !b' (the result of assignment). You need to return directly this from any methods that need to be chainable

vadimk7
  • 6,559
  • 1
  • 12
  • 15
1

this is what you need

Example

class Another {
  constructor() {
  }

  anotherMethod(a) {
    console.log('This is anohter method');
    this.a = a.replace('!d', 'd !e');
    return this;
  }

  otherMethod() {
    return super.otherMethod(this.a);
  }
}

class ChainAble extends Another {
  constructor() {
    super();
  }

  firstMethod() {
    this.a = 'a !b';
    return this;
  }

  secondMethod() {
    this.a = this.a.replace('!b', 'b !c');
    return this;
  }

  thirdMethod() {
    this.a = this.a.replace('!c', 'c !d');
    return this;
  }
  anotherMethod() {
    return super.anotherMethod(this.a);
  }
}

const chainableInstance = new ChainAble();
chainableInstance.firstMethod().secondMethod().thirdMethod().anotherMethod();

console.log(chainableInstance);
Józef Podlecki
  • 10,453
  • 5
  • 24
  • 50