【问题标题】:Chaining methods in JavascriptJavascript中的链接方法
【发布时间】:2010-11-09 07:33:48
【问题描述】:

我想在 Javascript 中链接方法(使用 Node.js)。

但是,我遇到了这个错误:

var User = {
    'deletes': function() {
        console.log('deletes');
        return this;
    },
    'file': function(filename) {
        console.log('files');
    }
};

User.deletes.file();


node.js:50
    throw e; // process.nextTick error, or 'error' event on first tick
    ^
TypeError: Object function () {
        console.log('deletes');
        return User;
    } has no method 'file'
    at Object.<anonymous> (/tests/isolation.js:11:14)
    at Module._compile (node.js:348:23)
    at Object..js (node.js:356:12)
    at Module.load (node.js:279:25)
    at Array.<anonymous> (node.js:370:24)
    at EventEmitter._tickCallback (node.js:42:22)
    at node.js:616:9

我怎样才能让它工作?

【问题讨论】:

    标签: javascript node.js chaining


    【解决方案1】:

    没有调用deletes 函数(该函数的字符串表示形式是错误跟踪中打印的内容)。

    试试:

    User.deletes().file()
    

    编码愉快。

    【讨论】:

      【解决方案2】:

      缺少一件事:User.deletes().file(&lt;filename&gt;)。我不确定,也许这会引发错误?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-28
        • 2013-02-04
        • 1970-01-01
        相关资源
        最近更新 更多