0

What is the best way to implement method chaining in javascript?

function CONSTRUCT(){
    this.func1=function(insert_ob){insert_ob.appendTo=this;/*other code*/};
    this.func2=function(...){};
    this.func3=function(...){};

}

function My_Object_Creator(){this.appendTo=null;}

object1=new CONSTRUCT();
my_object=new my_Object_Creator();
object1.func1(my_object).func2().func3();

I know I can adjust those functions to in the constructor to achieve it but I want to know if there are some well-known methods or patterns one should follow while creating method chaining possibility in javascript?

Jack_of_All_Trades
  • 10,942
  • 18
  • 58
  • 88

1 Answers1

0

The most basic way to method chain is to return this (or a reference to the object).

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445