Javascript中的globalThis是什么?理想的用例是什么? [英] What is globalThis in Javascript? What will be the ideal use case for this?

查看:264
本文介绍了Javascript中的globalThis是什么?理想的用例是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我在Java中遇到了有关globalThis的信息.我不确定如果从函数中调用它将如何表现.每次返回window对象.如果真是这样,那我们为什么不直接使用window对象.使用globalThis有什么必要?

Recently I have come across about globalThis in Javascript. I am not sure how it is going to behave if it is called from a function. Every time it is returning the window object. if that is the case, then why don't we directly use the window object. What is necessary of using globalThis?

如果我从函数调用,则它正在返回窗口对象 示例:

If I call the from a function, then it is returning window object Example:

(function test(){
    console.log(globalThis); // returns window
})();


var obj = {
    key1: function(){
        console.log(globalThis)
    },
    key2: ()=>{
        console.log(globalThis)
    },
    key3: function(){
        var arrFn = () => {
            console.log(globalThis);
        }
        arrFn();
    }
};

obj.key1(); // returns window object
obj.key2(); // returns window object
obj.key3(); // returns window object

我相信globalThis的内部实现类似于以下代码:

I believe the internal implementation of globalThis is like the below code:

const getGlobalThis = () => {
  if (typeof globalThis !== 'undefined') return globalThis;
  if (typeof self !== 'undefined') return self;
  if (typeof window !== 'undefined') return window;
  if (typeof global !== 'undefined') return global;
  // Note: this might still return the wrong result!
  if (typeof this !== 'undefined') return this;
  throw new Error('Unable to locate global `this`');
};
const theGlobalThis = getGlobalThis();

有人可以向我解释globalThis的确切用例吗?使用此功能的理想方案是什么?

Can anyone please explain to me the exact use case of the globalThis? What will be the ideal scenario to use this?

推荐答案

MDN说:

全局globalThis属性包含全局this值,类似于全局对象.

The global globalThis property contains the global this value, which is akin to the global object.

为什么有用:

从历史上看,访问全局对象在不同的​​JavaScript环境中需要使用不同的语法.在网络上,您可以使用windowselfframes-但是在Web Worker中,只有self可以使用.在Node.js中,这些都不起作用,您必须改为使用global.

Historically, accessing the global object has required different syntax in different JavaScript environments. On the web you can use window, self, or frames - but in Web Workers only self will work. In Node.js none of these work, and you must instead use global.

globalThis属性提供了一种跨环境访问全局"this"值(从而访问全局对象本身)的标准方法.与window和self之类的类似属性不同,它保证可以在window和非window上下文中工作.这样,您可以以一致的方式访问全局对象,而不必知道代码在哪个环境中运行.为了帮助您记住名称,只需记住在全局范围内此值是globalThis.

The globalThis property provides a standard way of accessing the global 'this' value (and hence the global object itself) across environments. Unlike similar properties such as window and self, it's guaranteed to work in window and non-window contexts. In this way, you can access the global object in a consistent manner without having to know which environment the code is being run in. To help you remember the name, just remember that in global scope the this value is globalThis.

如果您不确定要在什么环境中运行代码,或者不想跟踪它(毕竟,减少认知开销是一件好事!),可以使用globalThis代替. (不过,在足够多的浏览器上实现本机,以便在不使用polyfill的情况下使用本机可能要花相当长的时间)

If you don't know for certain what environment the code is going to be run in, or don't feel like keeping track of it (less cognitive overhead is a good thing, after all!), you can use globalThis instead. (though, it'll probably be quite a while before it's implemented natively on enough browsers to be used without a polyfill)

如果您确定您的代码将在哪个环境中运行,并且永远不会将代码移植到其他环境,请随时继续使用window(或该环境的相应其他属性)全局对象).

If you know for sure what environment your code is going to be running in, and that the code will never be ported to a different environment, feel free to just keep using window (or the appropriate other property for the environment's global object).

这篇关于Javascript中的globalThis是什么?理想的用例是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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