globalThis的低版本浏览器兼容

问题背景:

由于项目中用到了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_;
}());
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。