摆脱CoffeeScript的封装包装

Zep*_*lin 20 coffeescript

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

(function() {
  // my compiled code
}).call(this);
Run Code Online (Sandbox Code Playgroud)

只是玩弄CoffeeScript + SproutCore,当然,我更愿意保留原样:在这种情况下,没有必要保护任何东西不被覆盖.

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

the*_*ejh 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
Run Code Online (Sandbox Code Playgroud)

  • 这个答案是正确的,但值得注意的是,几乎在所有情况下都严重劝阻`-b`.[这就是原因](http://stackoverflow.com/questions/5211638/pattern-for-coffeescript-modules/5212449#5212449). (11认同)
  • 裸体不一定总是气馁.服务器端使用node.js有一个真实的用例 (4认同)

use*_*242 7

我使用了另一个选项,即将我的全局变量附加到函数范围内的全局对象.我附上了我的"窗口".这样可以封装JavaScript,并且只显示全局范围内所需的变量.