如何在CoffeeScript中定义全局变量? [英] How do I define global variables in CoffeeScript?

查看:143
本文介绍了如何在CoffeeScript中定义全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Coffeescript.org上:

On 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编译,所以:

compiling via coffee-script under node.js wraps that so:

(function() {
  var bawbag;
  bawbag = function(x, y) {
    var z;
    return (z = (x * y));
  };
  bawbag(5, 10);
}).call(this);

文件说:


如果您想要为其他脚本创建顶级变量,
将它们作为属性附加到窗口上,或者在
CommonJS中的exports对象上。存在操作符(如下所示)给你一个
的可靠方法来确定添加它们的位置,如果你同时定义
CommonJS和浏览器:root = exports?

If you'd like to create top-level variables for other scripts to use, attach them as properties on window, or on the exports object in CommonJS. The existential operator (covered below), gives you a reliable way to figure out where to add them, if you're targeting both CommonJS and the browser: root = exports ? this

如何在CoffeeScript中定义全局变量。

How do I define Global Variables then in CoffeeScript. What does 'attach them as properties on window' mean?

推荐答案

由于咖啡脚本没有 var 语句,它会自动将其插入coffee-script中的所有变量,这样就可以防止编译的JavaScript版本将所有内容泄漏到全局命名空间中。

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.

因此,由于没有办法让某些东西从咖啡脚本中泄漏到全局命名空间

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

这意味着你需要做一些 window.foo ='baz'; 它处理浏览器的情况,因为全局对象窗口

This means you need to do something like window.foo = 'baz';, which handles the browser case, since there the global object is the window.

在Node.js中没有窗口对象,而是 对象,它被传递到包装Node.js模块的包装器中(参见: https://github.com/ry/node/blob/master/src/node.js#L321 ),因此在Node.js中,您需要做的是 exports.foo ='baz';

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';.

现在让我们从文档中看看它在报价中的陈述: / p>

Now let us take a look at what it states in your quote from the docs:


...定位CommonJS和浏览器:root = exports?

...targeting both CommonJS and the browser: 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;

首先检查 exports ,因为试图在JavaScript中引用一个不存在的变量会产生一个SyntaxError(除非它与 typeof 一起使用)

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 存在,这是在Node.js(或在一个严重编写的WebSite ...)的情况下,根将指向 exports ,否则为。那么什么是这个

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. So what's this?

(function() {...}).call(this);

使用 .call 这个里面的函数中的第一个参数传递,在浏览器的情况下,现在 $ c> window 对象,如果是Node.js,它将是全局上下文,也可以作为全局 object。

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.

但由于您在Node.js中有 require 函数,因此不需要为< c $ c>全局对象,而是分配给 exports 对象,然后返回需要功能。

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.

你需要做:

root = exports ? this
root.foo = -> 'Hello World'

这将声明我们的函数 foo 在全局命名空间(无论发生什么)。

这是全部:)

This will declare our function foo in the global namespace (whatever that happens to be).
That's all :)

这篇关于如何在CoffeeScript中定义全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆