Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting rid of CoffeeScript's closure wrapper

Tags:

How can I omit the automatic closure wrappers that hides my variables from the global scope?

(function() {
  // my compiled code
}).call(this);

Just playing around with CoffeeScript+SproutCore, and of course, I'd prefer to leave the scope as it is: in this case there is no need to protect anything from overwriting.

I know I can use @ or this. at the declaration, but that's not too elegant.

like image 472
Zeppelin Avatar asked Apr 17 '11 11:04

Zeppelin


People also ask

What can you do with CoffeeScript?

CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and destructuring assignment.

How do I know if CoffeeScript is installed?

The coffee and cake commands will first look in the current folder to see if CoffeeScript is installed locally, and use that version if so. This allows different versions of CoffeeScript to be installed globally and locally.


2 Answers

Quick and dirty solution: Use the console flag -b (bare). Warning: Kittens will die if you do that!

Clean solution: Don't do that.

Usage: coffee [options] path/to/script.coffee

  -c, --compile      compile to JavaScript and save as .js files
  -i, --interactive  run an interactive CoffeeScript REPL
  -o, --output       set the directory for compiled JavaScript
  -j, --join         concatenate the scripts before compiling
  -w, --watch        watch scripts for changes, and recompile
  -p, --print        print the compiled JavaScript to stdout
  -l, --lint         pipe the compiled JavaScript through JSLint
  -s, --stdio        listen for and compile scripts over stdio
  -e, --eval         compile a string from the command line
  -r, --require      require a library before executing your script
  -b, --bare         compile without the top-level function wrapper
  -t, --tokens       print the tokens that the lexer produces
  -n, --nodes        print the parse tree that Jison produces
      --nodejs       pass options through to the "node" binary
  -v, --version      display CoffeeScript version
  -h, --help         display this help message
like image 181
thejh Avatar answered Oct 04 '22 00:10

thejh


I used another option which was to attach my global variables to the global object in the scope of my function. I attached mine to the 'window'. This keeps your JavaScript encapsulated and only exposes the variable that you need in the global scope.

like image 29
user699242 Avatar answered Oct 04 '22 00:10

user699242