分配给未声明的变量
请考虑以下代码。如果我按如下方式声明 exports
字段:
exports =
someFunc : -> # blablabla
someOtherFunc : ->
它会被编译为:
var exports;
exports = {
someFunc: function() {},
someOtherFunc: function() {}
};
但正如您可能已经知道的那样,我需要 Exports 字段保持未声明状态。换句话说,我需要以某种方式通知编译器不要生成 varexports; 语句。 我知道我可以像这样解决这个问题:
exports.someFunc = ->
exports.someOtherFunc = ->
但这很混乱,而且看起来像是一个缺陷,因为 CoffeeScript 的本质是减少代码噪音。
有没有办法或更好的解决办法?
Please, consider the following code. If I declare the exports
field as follows:
exports =
someFunc : -> # blablabla
someOtherFunc : ->
It gets compiled into:
var exports;
exports = {
someFunc: function() {},
someOtherFunc: function() {}
};
But as you probably already know I need the exports field to remain undeclared. In other words I need to somehow inform the compiler not to produce the var exports;
statement.
I know that I can hack around this like that:
exports.someFunc = ->
exports.someOtherFunc = ->
but that's just messy and very much seems like a defect, since the essence of CoffeeScript is to reduce the code noise.
Is there a way or a better hack around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你不能直接分配给导出(在nodejs中)。我认为你的代码应该是
在这种情况下 CS 会假设模块已经定义并且只会输出
I don't think you can assign directly to exports (in nodejs). I think your code should be
in which case CS will assume module is already defined and will simply output
我想到了一个更聪明的解决方案,它意外地更适合
export
需求,因为它不会删除之前分配的导出字段:但它仍然是一个 hack,它只能工作用于分配未声明对象的字段。如何分配未声明的变量仍然是一个问题。
I thought of one a bit more clever hack around this, which by accident suits the
export
needs much better, since it doesn't erase the previously assigned export fields:But still it's a hack and it only works for assigning the fields of undeclared objects. How to assign the undeclared variable remains a question.
我假设您“需要 Exports 字段保持未声明”,因为您正在编写 Node.js 模块
exports
已经声明,是吗?在这种情况下,这个问题及其最佳答案应该对您有很大的帮助: How do I Define Global Variables in CoffeeScript?简而言之,您将执行以下操作:
希望有帮助!
I'm assuming you "need the exports field to remain undeclared" because you're writing a Node.js module
exports
is already declared, yes? In that case this question and its top answer ought to go a long way toward helping you: How do I define global variables in CoffeeScript?In short you'll do something like this:
Hope that helps!