سؤال

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?

هل كانت مفيدة؟

المحلول 2

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.

نصائح أخرى

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() {}
};

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!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top