Rails wraps any coffee script in an anonymous function when compiling it, so this is actually valid.
So even something as simple as:
x = 1
in coffeescript, gets compiled to:
(function() {
var x;
x = 1;
}).call(this);
In rails javascript. Hence why your sample is compiling to an anonymous function within an anonymous function. Here's the sprockets code that does it:
https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/jst_processor.rb
You could use global variables to get around this, but I wouldn't recommend it, instead, look to create a module system.