在Coffeescript.org上:
bawbag = (x, y) ->
z = (x * y)
bawbag(5, 10)
将编译为:
var bawbag;
bawbag = function(x, y) {
var z;
return (z = (x * y));
};
bawbag(5, 10);
在node.js下通过coffee-script编译,这样包装:
(function() {
var bawbag;
bawbag = function(x, y) {
var z;
return (z = (x * y));
};
bawbag(5, 10);
}).call(this);
医生说:
如果您想创建顶级变量供其他脚本使用,
将它们作为属性附加到窗口上,或在导出对象上
CommonJS。存在操作符(下面将介绍)给出一个
如果你的目标是两者,这是一种可靠的方法,可以找出在哪里添加它们
CommonJS和浏览器:root = exports ?这
我如何定义全局变量然后在CoffeeScript。“将它们作为属性附加到窗口”是什么意思?
对我来说,@atomicules似乎有最简单的答案,但我认为它可以再简化一点。你需要在任何你想要全局的东西前面加上@,这样它就会编译成这个。任何东西,this指向全局对象。
所以…
@bawbag = (x, y) ->
z = (x * y)
bawbag(5, 10)
编译……
this.bawbag = function(x, y) {
var z;
return z = x * y;
};
bawbag(5, 10);
并在node.js提供的包装器内部和外部工作
(function() {
this.bawbag = function(x, y) {
var z;
return z = x * y;
};
console.log(bawbag(5,13)) // works here
}).call(this);
console.log(bawbag(5,11)) // works here
对我来说,@atomicules似乎有最简单的答案,但我认为它可以再简化一点。你需要在任何你想要全局的东西前面加上@,这样它就会编译成这个。任何东西,this指向全局对象。
所以…
@bawbag = (x, y) ->
z = (x * y)
bawbag(5, 10)
编译……
this.bawbag = function(x, y) {
var z;
return z = x * y;
};
bawbag(5, 10);
并在node.js提供的包装器内部和外部工作
(function() {
this.bawbag = function(x, y) {
var z;
return z = x * y;
};
console.log(bawbag(5,13)) // works here
}).call(this);
console.log(bawbag(5,11)) // works here