How do I create method chaining functionality similar to jQuery in JavaScript. For eg. I have a main function where I can add different methods to it like add, subtract, multiply, divide, while main function taking one param.
Main(5).add(3).subtract(2).multiply(4).divide(6)
or
Main(10).subtract(5).add(5).divide(6)
It can be anything.
I was trying to put methods in an object in the return statement of the parent function, but it didn’t work.
function main(a) {
return {
sum: function (b) {
return a + b;
},
substract: function (c) {
return a - c;
},
};
}
Please help