JavaScript - What is the best method when creating chainable functions?

javascript, return, return-value

Solution

You can make a chainable functions like this:

var test = function(){

    var self = {};
    console.log('test called')

    function someFunc() {
        console.log('someFunc')
        return self;
    }

    function someOtherFunc() {
        console.log('someOtherFunc')
        return self;
    }   

    self.someFunc = someFunc;
    self.someOtherFunc = someOtherFunc;
    return self;

}

test().someFunc().someOtherFunc();

Hope this helps

Problem

EDIT Here is an attempt to make my question simpler. ``` return this.someFunc(); == return { XXX:this.someFunc() }; ``` What do I have to put in for XXX to make this statement true? I'm trying to create a function that can be chained. Let me write some hypothetical code. (Any syntax errors just ignore, I'm typing this fast and this is just concept code.) Assume that all functions are either defined locally or globally. :) ``` test = function(){ this.someFunc = function(){ this.retest = function(){ //...code } //...code } this.someFunc2 = function(){ //...code } return this.someFunc(); } ``` This function allows me to chain: `test().retest();` But what I want to do is return more than one item. ``` test = function(){ this.someFunc = function(){ this.retest = function(){ //...code } //...code } this.someFunc2 = function(){ //...code } return { XXX:this.someFunc(), //What do I put for XXX next:this }; } ``` I want to do this to access another function that test() offers: `test().next.someFunc2();` So my problem is this: I still want to be able to chain like this: `test().retest();` But I have to do it like this: `test().XXX.retest();` In my code, what is the name that I can put instead of XXX to accomplish this? And is this even possible? I have tried `0` and `default` already. Thanks for the help.

Original source