CoffeeScript:coffee -w name-of-file.coffee抱怨:“window is not defined” [英] CoffeeScript: coffee -w name-of-file.coffee complains: “window is not defined”
问题描述
在CofeeScript中,我通过这样做创建一个全局对象:
In CofeeScript I am creating a global object by doing this:
window.App =
init : ->
...
运行 coffee -w app.coffee
抱怨窗口未定义
,不会重写 app.js
文件。
Running coffee -w app.coffee
complains window is not defined
and doesn't rewrite the app.js
file.
但是,运行 coffee -c app.coffee
编译没有问题。
如何获得咖啡-w
接受全局窗口
?
However, running coffee -c app.coffee
compiles without a problem.
How can I get coffee -w
to accept global window
?
CoffeeScript版本是1.1.1(来自 coffee -v
)
CoffeeScript version is 1.1.1 (from coffee -v
)
谢谢! / p>
Thanks!
推荐答案
如果你想观看一个文件并编译它,你需要做:
If you want to watch a file and have it compiled you need to do:
coffee -wc file.coffee
只使用 -w
标志导致咖啡只是在脚本运行时运行脚本,如同运行:
Using only the -w
flag causes coffee to just run the script when it changes, as if you had run:
coffee file.coffee
关于窗口没有定义
错误,如果你想让你的脚本在浏览器和node.js中运行,那么你可以这样做:
In regards to the window is not defined
error, if you want to make your script runnable both in a browser and in node.js, then you can do this:
root = exports ? this
class Thing
constructor: (@name) ->
whoAreYou: ->
alert @name
root.Thing = Thing
有用的标志组合是 -wp
,它只是管道编译javascript标准输出每次你对文件进行更改。
Another useful flag combination is -wp
which just pipes the compile javascript to standard out each time you make a change to the file.
这篇关于CoffeeScript:coffee -w name-of-file.coffee抱怨:“window is not defined”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!