0

i'm looking for something like :

document.getElementById("id").innerText.toString().length;

so : class.method1().method2().method3();

my code :

class Example
{
    method1(){ .. }
    method2(){ .. }
    method3(){ .. }
}

from out class :

var element = new Example();

element.method1(); //works fine
element.method1().method2(); // error: method2 is not defined

any solution?

thanks for all :)

  • That's because you're calling `.method2` on `element.method1()` (=the return value of `method1`) and not `element` itself – Nino Filiu Feb 21 '21 at 20:21

1 Answers1

2

What you're looking for is known as a fluent interface. To implement this, just add return this; to the end of every method in the class.

Side note: document.getElementById("id").innerText.toString().length; is not a fluent interface - each function/attr returns a different value.

SuperStormer
  • 4,997
  • 5
  • 25
  • 35