Last active
October 22, 2019 07:56
-
-
Save shenqihui/f44958fc216bb24ba4aa7fe319aadf2c to your computer and use it in GitHub Desktop.
微信小程序获取 global 全局调试对象,直接控制台访问某个值调试 ,解决 lodash 的问题。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import './global'; | |
import debugAdd, { globalAdd } from './debug'; | |
App({ | |
// ........ 一大堆的东西 | |
}); | |
const app = getApp(); | |
debugAdd('app', app); // 直接控制台 console 那边 debugAddSave.app 查看内存 | |
globalAdd('app', app);// 直接控制台 console 那边 app 查看内存 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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