Coffeescript 将您的代码包装在一个函数调用中,这样您就不会意外覆盖全局变量。
如果您希望任何变量、函数或类是全局的(以便其他文件可以访问它们),您需要通过将它们附加到this
or来显式地使它们成为全局的window
。
# Stays within the function scope, so you can't access it outside the file
myNotGlobalFunction -> return
# Attaches it to `this` aka `window`, so can be accessed globally
this.myGlobalFunction -> return
# A shortcut using @ which is an alias to `this.`
@myOtherGlobalFunction -> return
这编译为:
(function() {
myNotGlobalFunction(function() {
return;
});
this.myGlobalFunction(function() {
return;
});
this.myOtherGlobalFunction(function() {
return;
});
}).call(this);