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_;
}());