Why use the javascript function wrapper (added in coffeescript) “.call(this)”

It’s a way to make sure that the compiled CoffeeScript has its own scope for variable names. This has benefits in terms of efficiency and simplicity (you know you the generated JavaScript won’t stomp on variables used by other code). You can disable it with the --bare (or -b) option to the CoffeeScript compiler.

The reason for the call(this) is just to ensure that the CoffeeScript has the same this as the scope where it’s placed, because functions don’t normally inherit their this object from the surrounding context.

Leave a Comment