20

如何省略将变量隐藏在全局范围内的自动闭包包装器?

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

只是在使用 CoffeeScript+SproutCore,当然,我更愿意保持原样:在这种情况下,不需要保护任何内容不被覆盖。

我知道我可以在声明中使用@or this.,但这并不太优雅。

4

2 回答 2

42

快速而肮脏的解决方案:使用控制台标志-b(裸)。警告:如果你这样做,小猫会死!

清洁解决方案:不要那样做。

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 回答