ForbesLindesay
ForbesLindesay

Reputation: 10712

Why does running the CoffeeScript compiler multiple times produce different output?

I'm attempting to compile the very simple coffee script:

n = 4;

Using the code

require('coffee-script').compile(str);

I do this multiple times in my solution, without caching the results. The first time I do this, I get:

(function() {
    var n;
    n = 4;
}).call(this);

but if I then call it again, I get:

n = 4;

Why don't I get the wrapper function the second time round, and how can I make it give the same results every time.

Upvotes: 2

Views: 138

Answers (3)

Ricardo Tomasi
Ricardo Tomasi

Reputation: 35253

Can't reproduce, I get the same (wrapped) code everytime.

Use coffee.compile(str, { bare: false }) to enforce the closure wrapper.

Upvotes: 1

devmiles.com
devmiles.com

Reputation: 10014

Compile just didn't work for some reason the second time around. CoffeeScript code is always wrapped in a function.

Upvotes: 0

shytikov
shytikov

Reputation: 9548

I believe it might happen (i.e. different outputs) due to scope issues.

Normally you need only one var statement for variable. And it seems to be that during the first call all needed declarations were created and second call just reuses them.

I believe it might happen due to fact that the same require('coffee-script') instance is used.

Upvotes: 0

Related Questions