Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

javascript - 如何在CoffeeScript中定义全局变量?(How do I define global variables in CoffeeScript?)

On Coffeescript.org:(在Coffeescript.org上:)

bawbag = (x, y) ->
    z = (x * y)

bawbag(5, 10) 

would compile to:(将编译为:)

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

compiling via coffee-script under node.js wraps that so:(通过在node.js下的coffee-script进行编译,可以这样包装:)

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

Docs say:(文件说:)

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.(如果要创建供其他脚本使用的顶级变量,请将它们作为属性附加到窗口或CommonJS中的exports对象上。)

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 ?(如果您同时针对CommonJS和浏览器,那么存在运算符(见下文)为您提供了一种可靠的方法来确定将它们添加到何处:root = exports?) this(这个)

How do I define Global Variables then in CoffeeScript.(然后如何在CoffeeScript中定义全局变量。)

What does 'attach them as properties on window' mean?(“将它们作为窗口的属性附加”是什么意思?)   ask by Handloomweaver translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :)(就这样 :))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...