Since coffee script has no var
statement it automatically inserts it for all variables in the coffee-script, that way it prevents the compiled JavaScript version from leaking everything into the global namespace .(由于coffee脚本没有var
语句,因此会自动将其插入coffee脚本中的所有变量,这样可以防止已编译的JavaScript版本将所有内容泄漏到全局命名空间中 。)
So since there's no way to make something "leak" into the global namespace from the coffee-script side of things on purpose, you need to define your global variables as properties of the global object .(因此,由于没有办法故意从咖啡脚本方面使某些内容“泄漏”到全局名称空间中 ,因此您需要将全局变量定义为全局对象的属性。)
attach them as properties on window(将它们作为属性附加在窗口上)
This means you need to do something like window.foo = 'baz';
(这意味着您需要执行诸如window.foo = 'baz';
)
, which handles the browser case, since there the global object is the window
.(,它处理浏览器的情况,因为那里的全局对象是window
。)
Node.js(Node.js)
In Node.js there's no window
object, instead there's the exports
object that gets passed into the wrapper that wraps the Node.js module (See: https://github.com/ry/node/blob/master/src/node.js#L321 ), so in Node.js what you would need to do is exports.foo = 'baz';
(在Node.js中没有window
对象,而是有exports
对象传递到包装Node.js模块的包装器中(请参阅: https : //github.com/ry/node/blob/master/src/node。 js#L321 ),因此在Node.js中,您需要做的是exports.foo = 'baz';
)
.(。)
Now let us take a look at what it states in your quote from the docs:(现在,让我们看一下它在文档中的报价中指出的内容:)
...targeting both CommonJS and the browser: root = exports ?(...针对CommonJS和浏览器:root = exports?)
this(这个)
This is obviously coffee-script, so let's take a look into what this actually compiles to:(显然,这是咖啡脚本,因此让我们看一下它实际编译成的内容:)
var root;
root = (typeof exports !== "undefined" && exports !== null) ? exports : this;
First it will check whether exports
is defined, since trying to reference a non existent variable in JavaScript would otherwise yield an SyntaxError (except when it's used with typeof
)(首先,它将检查是否定义了exports
,因为尝试在JavaScript中引用不存在的变量会否则产生SyntaxError(与typeof
使用时除外))
So if exports
exists, which is the case in Node.js (or in a badly written WebSite...) root will point to exports
, otherwise to this
.(因此,如果存在exports
,则在Node.js(或写得不好的WebSite ...)中就是这种情况,根将指向exports
,否则指向this
。)
So what's this
?(那么,什么是this
?)
(function() {...}).call(this);
Using .call
on a function will bind the this
inside the function to the first parameter passed, in case of the browser this
would now be the window
object, in case of Node.js it would be the global context which is also available as the global
object.(使用.call
上的功能将结合this
功能的第一个参数中过去了,在浏览器的情况下, this
现在是该window
中的Node.js的情况下对象,这将是全球范围内它也可作为global
对象。)
But since you have the require
function in Node.js, there's no need to assign something to the global
object in Node.js, instead you assign to the exports
object which then gets returned by the require
function.(但是,由于在Node.js中具有require
函数,因此无需在Node.js中为global
对象分配某些内容,而是将其分配给exports
对象,然后由require
函数返回该对象。)
Coffee-Script(咖啡脚本)
After all that explanation, here's what you need to do:(经过所有这些说明之后,您需要执行以下操作:)
root = exports ? this
root.foo = -> 'Hello World'
This will declare our function foo
in the global namespace (whatever that happens to be).(这将在全局名称空间中声明我们的函数foo
(无论发生什么情况)。)
That's all :)(就这样 :))