美文网首页
JS获取顶层对象

JS获取顶层对象

作者: last_edc | 来源:发表于2016-12-15 14:54 被阅读43次

    方法一

    ‘(typeof window !== 'undefined' ? window : (typeof process === 'object' && typeof require === 'function' && typeof global === 'object') ? global : this);’

    方法二

    var getGlobal = function () {
    if (typeof self !== 'undefined') { return self; } 
    if (typeof window !== 'undefined') { return window; } 
    if (typeof global !== 'undefined') { return global; } 
    throw new Error('unable to locate global object');
    };
    

    方法三

    使用system.global

    // CommonJS的写法
    var global = require('system.global')();
    // ES6模块的写法
    import getGlobal from 'system.global';
    const global = getGlobal();

    相关文章

      网友评论

          本文标题:JS获取顶层对象

          本文链接:https://www.haomeiwen.com/subject/slydmttx.html