jashkenas/coffee-script — pull request #2634 by jcorbin (2012-12-01 20:29:58+00:00)
Title: Add noglobals compile option to generate self-contained functions

### The noglobals compile option

These changes make it easier to to pass generated code on to other systems,
such as MongoDB.

Consider a reduce function:

```coffeescript
(key, [result, values...]) ->
for value in values
result = binop result, value
result
```

Without noglobals:

```javascript
var __slice = [].slice;

(function(key, _arg) {
var result, value, values, _i, _len;
result = _arg[0], values = 2 <= _arg.length ? __slice.call(_arg, 1) : [];
for (_i = 0, _len = values.length; _i < _len; _i++) {
value = values[_i];
result = binop(result, value);
}
return result;
});
```

With noglobals:

```javascript
(function(key, _arg) {
var result, value, values, _i, _len,
__slice = [].slice;
result = _arg[0], values = 2 <= _arg.length ? __slice.call(_arg, 1) : [];
for (_i = 0, _len = values.length; _i < _len; _i++) {
value = values[_i];
result = binop(result, value);
}
return result;
});
```

### Classes with external constructors

Consider:

```coffeescript
class Foo
constructor: Whatever
```

Curently this compiles to:

```javascript
var Foo, _class;

Foo = (function() {

function Foo() {
return _class.apply(this, arguments);
}

_class = Whatever;

return Foo;

})();
```

After SHA: 346c2da25f65d738fd833cfecf2688010d7b63f7

```javascript
var Foo;

Foo = (function() {
var constructor;

function Foo() {
return constructor.apply(this, arguments);
}

constructor = Whatever;

return Foo;

})();
```

While I was at it, I fixed a minor edge case in SHA: 847f9c4e7f8ebefbd9dc0c8a664ec480ee6a9508.

### Potential next steps

It would be nice to be able to tighten down the 'var ...' declarationsin the
generated code too. However this wasn't necenssary in my current application,
so I note it mainly as an idle curiosity.

I considered introducing a similar constructor closure variable for internal
constructors too, but that seemed a bit like scope creep given my goals.