Skip to content

Instantly share code, notes, and snippets.

@shenqihui
Last active October 22, 2019 07:56
Show Gist options
  • Save shenqihui/f44958fc216bb24ba4aa7fe319aadf2c to your computer and use it in GitHub Desktop.
Save shenqihui/f44958fc216bb24ba4aa7fe319aadf2c to your computer and use it in GitHub Desktop.
微信小程序获取 global 全局调试对象,直接控制台访问某个值调试 ,解决 lodash 的问题。
import './global';
import debugAdd, { globalAdd } from './debug';
App({
// ........ 一大堆的东西
});
const app = getApp();
debugAdd('app', app); // 直接控制台 console 那边 debugAddSave.app 查看内存
globalAdd('app', app);// 直接控制台 console 那边 app 查看内存
import global from './global';
function debugAdd(key, memory) {
global.debugAddSave = global.debugAddSave || {};
global.debugAddSave[key] = memory;
}
global.debugAdd = debugAdd;
function globalAdd(key, memory) {
global[key] = memory;
}
global.globalAdd = globalAdd;
export default debugAdd;
export { globalAdd, debugAdd };
/* eslint-disable */
// add by shenqihui@ghzhsng.com
// https://zhuanlan.zhihu.com/p/52797872 小程序的全局变量
// 需要在 app.js 的第一行引入 import './global';
Object.defineProperty(Object.prototype, '__root', {
get() {
if (this && this.App === App) {
return this;
}
},
configurable: false,
enumerable: false,
});
// global 是为了解决 lodash 的问题。估计会有后遗症
var globalFlagArr = ['self', 'global', 'root'];
for (var i = 0; i < globalFlagArr.length; i++) {
__root[globalFlagArr[i]] = __root;
}
module.exports = __root;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment