miniprogram-polyfill
miniprogram-polyfill copied to clipboard
小程序环境,设置全局变量,拿不到globalThis解决办法
支付宝小程序IDE环境目前globalThis为undefined,真机环境下是存在的,下面是个兼容的解决方法:
if (typeof globalThis !== 'object') {
Object.defineProperty(Object.prototype, 'globalThis', {
get() {
return this
}
})
}
字节小程序目前不存在globalThis,下面是设置全局变量的解决方法: 该方法只在IDE环境可用,真机环境不可用,并且目前没有找到解决方法。
if (typeof globalThis !== "object") {
// TIP: 下方变量名不能设置为"globalThis"
Object.defineProperty(Object.prototype, "globalThis_", {
get() {
return this;
},
});
}
globalThis_.xxx = xxx;