简体   繁体   English

Javascript中的链接方法

[英]Chaining methods in Javascript

I want to chain methods in Javascript (using Node.js). 我想在Javascript中链接方法(使用Node.js)。

However, I encountered this error: 但是,我遇到此错误:

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

How could I make it work? 我该如何运作?

You are not invoking the deletes function (the string representation of the function is what is printed in the error trace). 没有在调用 deletes函数(该函数的字符串表示形式是错误跟踪中打印的内容)。

Try: 尝试:

User.deletes().file()

Happy coding. 快乐的编码。

One thing is missing: User.deletes().file(<filename>) . 缺少一件事: User.deletes().file(<filename>) I'm not sure, maybe this raise an error? 我不确定,也许这会引发错误?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM