globalThis的低版本浏览器兼容

2023-07-23  本文已影响0人  xiaoguo16

问题背景:

由于项目中用到了async写法,需要引入runtime.js才可以兼容该写法。
但是runtime.js中用到了globalThis,在chrome60浏览器中无法识别,报错了,错误为globalThis is not defined

问题解决:

手工写一个polyfill,兼容globalThis
代码如下

(function(){
 if(typeof globalThis === 'object') return;
 Object.defineProperty(Object.prototype, '_magic_', {
  get: function(){
    return this;
  },
  configurable:true
 });
  _magic_.globalThis = _magic_;
  delete Object.prototype._magic_;
}());
上一篇 下一篇

猜你喜欢

热点阅读