分配给未声明的变量

发布于 2025-01-05 23:45:46 字数 529 浏览 1 评论 0原文

请考虑以下代码。如果我按如下方式声明 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦巷 2025-01-12 23:45:46

我认为你不能直接分配给导出(在nodejs中)。我认为你的代码应该是

module.exports = 
  someFunc : -> # blablabla
  someOtherFunc : ->

在这种情况下 CS 会假设模块已经定义并且只会输出

module.exports = {
  someFunc: function() {},
  someOtherFunc: function() {}
};

I don't think you can assign directly to exports (in nodejs). I think your code should be

module.exports = 
  someFunc : -> # blablabla
  someOtherFunc : ->

in which case CS will assume module is already defined and will simply output

module.exports = {
  someFunc: function() {},
  someOtherFunc: function() {}
};
傲娇萝莉攻 2025-01-12 23:45:46

我想到了一个更聪明的解决方案,它意外地更适合 export 需求,因为它不会删除之前分配的导出字段:

exports[k] = v for k, v of {
  someFunc : ->
  someOtherFunc : ->
}

但它仍然是一个 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:

exports[k] = v for k, v of {
  someFunc : ->
  someOtherFunc : ->
}

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.

最丧也最甜 2025-01-12 23:45:46

我假设您“需要 Exports 字段保持未声明”,因为您正在编写 Node.js 模块 exports 已经声明,是吗?在这种情况下,这个问题及其最佳答案应该对您有很大的帮助: How do I Define Global Variables in CoffeeScript?

简而言之,您将执行以下操作:

root = exports ? this

root = 
  someFunc      : -> # blablabla
  someOtherFunc : ->

希望有帮助!

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:

root = exports ? this

root = 
  someFunc      : -> # blablabla
  someOtherFunc : ->

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文