Javascript 中的链接方法
我想在 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
我怎样才能使它工作?
I want to chain methods in Javascript (using 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有调用
deletes
函数(该函数的字符串表示形式是错误跟踪中打印的内容)。尝试:
快乐编码。
You are not invoking the
deletes
function (the string representation of the function is what is printed in the error trace).Try:
Happy coding.
缺少一件事:
User.deletes().file()
。我不确定,也许这会引发错误?One thing is missing:
User.deletes().file(<filename>)
. I'm not sure, maybe this raise an error?