命名空间内的方法链

Method chaining within namespace

我不知道如何在同一个命名空间内链接方法(我不想创建 class 而是直接调用它):

var namespace = {
    one: function(args) {
        // do something
    },
    two: function() {
        // do something in addition
    }
}

// call both
namespace.one(true).two();

您需要 return namespacethis

var namespace = {
    one: function(args) {
        // do something
        console.log('one');
        return this;
    },
    two: function() {
        // do something in addition
        console.log('two');
        return this;
    }
}

// call both
namespace.one(true).two();

您需要 return 对命名空间对象的引用才能链接它。

var namespace = {
    one: function (args) {
       // Do something
       return this;
    },
    two: function () {
       // Do something
       return this;
    }
}