节点中的全局变量是什么?

时间:2017-03-09 21:30:25

标签: javascript node.js

而不是写

var global = window

对于浏览器我希望我的代码能够在节点环境中工作。

这样的东西
var global = window || node_global

什么是node_global

这里没有看到任何明显的东西:

https://stackoverflow.com/search?q=global+node+variable

或在这里

https://www.google.com/?gws_rd=ssl#q=node+global+variable++& *

代码

// set your global variable with glob
Pub.globalManager = (function () {
    var global = window;
    var glob = "$A";
    var previous = global[glob];
    var pub = {};

    // package name is set here
    // this is the first component
    Pub.pack = {
        utility: true
    };

    // set the global property
    pub.release = function () {
        global[glob] = Pub.extendSafe(global[glob] || {}, Pub);
    };

    // return the global property back to its original owner
    pub.noConflict = function () {
        var temp = global[glob];
        global[glob] = previous;
        return temp;
    };
    return pub;
}());

1 个答案:

答案 0 :(得分:1)

There is already a TC39 proposal to add global to ECMAScript。或者,您可以使用此npm模块system.global

它是polyfill

'use strict';
(function (global) {
    if (!global.global) {
        if (Object.defineProperty) {
            Object.defineProperty(global, 'global', {
                configurable: true,
                enumerable: false,
                value: global,
                writable: true
            });
        } else {
            global.global = global;
        }
    }
})(typeof this === 'object' ? this : Function('return this')())