zen
zen

Reputation: 317

What is the global variabel in node?

Instead of writing

var global = window

for the browser I want my code to be able to work in a node enviornment as well.

Something like

var global = window || node_global

What is node_global?

Did not see anything obvious here:

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

or here

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

Code

// 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;
}());

Upvotes: 0

Views: 46

Answers (1)

idbehold
idbehold

Reputation: 17168

There is already a TC39 proposal to add global to ECMAScript. Alternatively you can use this npm module system.global.

And it's 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')())

Upvotes: 1

Related Questions