I trying to create reusable chaining function, but I'm stuck. Common way c.plus(5).plus(2).execute() work fine, but I have no idea how to make this reusable like below. Do you have any idea how to do this?
function chain() {
this.i = 0;
this.plus = (x) => {
this.i = this.i + x;
return this;
};
this.execute = () => console.log(this.i);
}
const c = new chain();
const c1 = c.plus(5);
c1.plus(2).execute(); // 7
c1.execute();// 7 instead of 5