在JavaScript中定义全局对象的实现独立版本 [英] Defining an implementation independent version of the global object in JavaScript

查看:88
本文介绍了在JavaScript中定义全局对象的实现独立版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JavaScript中的一行中定义global对象,如下所示:

var global = this.global || this;

以上陈述是在全球范围内.因此,在浏览器中,this指针是window对象的别名.假定这是要在当前网页的上下文中执行的第一行JavaScript,global的值将始终与this指针或window对象的值相同.

在CommonJS实现中,例如RingoJS和node.js,this指针指向当前的ModuleScope.但是,我们可以通过ModuleScope上定义的属性global访问global对象.因此,我们可以通过this.global属性访问它.

因此,此代码段适用于所有浏览器,至少适用于RingoJS和node.js,但我尚未测试其他CommomJS实现.因此,我想知道在任何其他CommonJS实现上运行时,此代码是否不会产生正确的结果,如果可以的话,我该如何解决.

最终,我打算在lambda表达式中将其用于与实现无关的JavaScript框架,如下所示(来自jQuery的想法):

(function (global) {
    // javascript framework
})(this.global || this);

解决方案

阅读Esailija和Raynos的答案后,我了解到我的代码this.global || this不适用于node.js中的所有情况;如果全局范围中已经存在名为global的变量,它甚至可能在浏览器中失败.

Esailija指出this.global并不是真正的global对象,而是指出this是RingoJS中的global对象.尽管我理解他的论点,但出于我的目的,我需要this.global而不是this.

Raynos建议我为每个CommonJS环境进行硬代码功能检测.但是,由于我目前仅支持RingoJS和node.js,因此只需要测试globalwindow.因此,我决定坚持使用this.global || this.

但是,正如我从benvie的评论中了解到的那样,正如我之前所说的,this.global || this不适用于node.js中的所有情况.在node.js REPL中,我意识到我需要this而不是this.global.但是,this.global || this表示this.global.在node.js模块中,我需要this.global而不是this.但是,由于this.globalundefined,因此它表示this.因此,为了解决这个问题,我最终决定使用以下代码:

(function (global) {
    // javascript framework
})(typeof global !== "undefined" && global || this);

我使用此代码的原因是因为在node.js模块中,this.globalundefined.因此,我们必须直接使用global.因此,我们在RingoJS和node.js中都使用typeof global !== "undefined" && global来获取global对象.并且我们将this用作浏览器(window)中的global对象,并用作默认后备.

注意:我没有为在node.js REPL中找到global对象提供任何逻辑,因为我不相信我的框架还是会直接在REPL中使用.但是,一旦理解了Benvie指出的那样,一旦理解了在node.js中找到global对象的复杂性,编写查找逻辑的逻辑就相当简单了.我知道我不知道.

I'm trying to define the global object in JavaScript in a single line as follows:

var global = this.global || this;

The above statement is in the global scope. Hence in browsers the this pointer is an alias for the window object. Assuming that it's the first line of JavaScript to be executed in the context of the current web page, the value of global will always be the same as that of the this pointer or the window object.

In CommonJS implementations, such as RingoJS and node.js the this pointer points to the current ModuleScope. However, we can access the global object through the property global defined on the ModuleScope. Hence we can access it via the this.global property.

Hence this code snippet works in all browsers and in at least RingoJS and node.js, but I have not tested other CommomJS implementations. Thus I would like to know if this code will not yield correct results when run on any other CommonJS implementation, and if so how I may fix it.

Eventually, I intend to use it in a lambda expression for my implementation independent JavaScript framework as follows (idea from jQuery):

(function (global) {
    // javascript framework
})(this.global || this);

解决方案

After reading Esailija and Raynos' answers I understood that my code this.global || this will not work for all cases in node.js; and that it may even fail in browsers if a variable called global already exists in the global scope.

Esailija pointed out that this.global is not really the global object, stating instead that this is the global object in RingoJS; and although I understand his arguments, for my purposes I require this.global and not this.

Raynos suggested that I hard code feature detection for every CommonJS environment. However since I'm currently only supporting RingoJS and node.js, I only need to test for global and window. Hence I decided to stick with this.global || this.

Nevertheless, as I said before this.global || this doesn't work for all cases in node.js as I understood from benvie's comments. In the node.js REPL I realized that I require this and not this.global. However, this.global || this expresses this.global. In a node.js module I require this.global and not this. However, it expresses this since this.global is undefined. Hence to solve this problem I finally decided to use the following code:

(function (global) {
    // javascript framework
})(typeof global !== "undefined" && global || this);

The reason I'm using this code is because in node.js modules this.global is undefined. Hence we must use global directly. Thus we use typeof global !== "undefined" && global to get the global object in both RingoJS and node.js; and we use this as the global object in browsers (window) and as a default fallback.

Note: I didn't provide any logic for finding the global object in the node.js REPL because I don't believe that my framework will be used directly within the REPL anyway. However, writing the logic to find it should be fairly trivial once one understands the complications of finding the global object in node.js as benvie pointed out. I know that I don't.

这篇关于在JavaScript中定义全局对象的实现独立版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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