取到顶层对象的两种方法 Gary Wang 2022-08-11 前端 js ES6 1(typeof window !== 'undefined' ? window : (typeof process === 'object' && typeof require === 'function' && typeof global === 'object')? global : this); 123456var getGlobal = function(){ if(typeof self !== 'undefined'){return self;} if(typeof window !== 'undefined'){return window;} if(typeof global !== 'undefined'){return global;} throw new Error('无法定位全局对象');};