20

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.

4

2 に答える 2

42

手っ取り早い解決方法: コンソール フラグ-b(bare) を使用します。警告: そんなことをしたら子猫は死んでしまいます!

きれいな解決策:そうしないでください。

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
于 2011-04-17T13:19:57.530 に答える
7

関数のスコープ内でグローバル変数をグローバル オブジェクトにアタッチする別のオプションを使用しました。私は「窓」に私のものを付けました。これにより、JavaScript がカプセル化されたままになり、グローバル スコープで必要な変数のみが公開されます。

于 2012-09-04T13:32:08.517 に答える