Javascript函数链接

Rly*_*now 1 javascript chaining node.js

我经常看到像这样的函数链:

db.find('where...')
.success(function(){...})
.error(function(error){...});
Run Code Online (Sandbox Code Playgroud)

我正在为我的项目工作验证库,我想知道我该怎么做这样的链接.问候

JDw*_*yer 7

只需从函数调用中返回正在操作的对象.

function MyObject(x, y) {
    var self = this;
    self.x = x;
    self.y = y;
    return {
        moveLeft: function (amt) {
            self.x -= amt;
            return self;
        },
        moveRight: function (amt) {
            self.x += amt;
            return self;
        }
    }
}
var o = MyObject(0, 0);
o.moveLeft(5).moveRight(3);
Run Code Online (Sandbox Code Playgroud)