0

在开发环境中,我的咖啡脚本文件以某种方式正确编译。但是当我将它们编译用于生产时,我会得到这样的东西

咖啡脚本:

$->
  alert "hello world"

编译为 Javascript

(function() {
   $(function(){
      alert("hello world");
   })
}).call(this)

我检查了是否有缺失的缩进和间距错误,或者是否混合了制表符和空格,但没有。奇怪的是,当我使用来自 coffeescript.org 的编译器转换它时,它可以正确编译,它只是在生产环境中。有任何想法吗?

顺便说一句:我正在使用rails 4

4

1 回答 1

0

It's a coffeescript setting.

(function() {
  # Code here
}).call(this)

Is a closure generated by coffeescript by default (can be disabled, but you shouldn't), used to avoid global namespace pollution.
It doesn't affect the script execution, your jQuery code will still be run once document is loaded.

Important note
The only issue you may find with that closure is that you actually have hard time declaring global variables. This can be solved in this way:

window.yourvar = 'something'

There is also a suggestion here on how you can disable it anyway: How can I use option "--bare" in Rails 3.1 for CoffeeScript?

于 2013-11-07T19:55:39.010 回答