在Coffeescript.org上:
bawbag = (x, y) ->
z = (x * y)
bawbag(5, 10)
Run Code Online (Sandbox Code Playgroud)
会编译为:
var bawbag;
bawbag = function(x, y) {
var z;
return (z = (x * y));
};
bawbag(5, 10);
Run Code Online (Sandbox Code Playgroud)
通过node.js下的coffee-script编译包装如下:
(function() {
var bawbag;
bawbag = function(x, y) {
var z;
return (z = (x * y));
};
bawbag(5, 10);
}).call(this);
Run Code Online (Sandbox Code Playgroud)
文件说:
如果要为其他要使用的脚本创建顶级变量,请将它们作为属性附加到窗口或CommonJS中的exports对象上.存在运算符(如下所述)为您提供了一种可靠的方法来确定添加它们的位置,如果您的目标是CommonJS和浏览器:root = exports?这个
如何在CoffeeScript中定义全局变量."将它们作为窗口上的属性附加"是什么意思?
我在Coffeescript FAQ上找到了这个片段,用于创建简单的命名空间.
# Code:
#
namespace = (target, name, block) ->
[target, name, block] = [(if typeof exports isnt 'undefined' then exports else window), arguments...] if arguments.length < 3
top = target
target = target[item] or= {} for item in name.split '.'
block target, top
# Usage:
#
namespace 'Hello.World', (exports) ->
# `exports` is where you attach namespace members
exports.hi = -> console.log 'Hi World!'
namespace 'Say.Hello', (exports, top) ->
# `top` is a reference to the main namespace …Run Code Online (Sandbox Code Playgroud)