JSFiddle - React, Tailwind, and code Playground

by Garrick Cheung

JavaScript

var namespace = (function() {
    var ns = {};
    return {
        get: function(path){
            var object = ns;
            if (typeof path == 'string') path = path.split('.');
            for (var i = 0, l = path.length; i < l; i++){
                if (object.hasOwnProperty(path[i])) object = object[path[i]];
                else return object[path[i]];
            }
            return object;
        },

        set: function(path, value){
            var object = ns;
            path = (typeof path == 'string') ? path.split('.') : path.slice(0);
            var key = path.pop(),
                len = path.length,
                i = 0,
                current;
            while (len--){
                current = path[i++];
                object = current in object ? object[current] : (object[current] = {});
            }
            object[key] = value;
        }
    }
})();

namespace.set('foo.bar.baz', {w00fz:'xxx'});
alert(namespace.get('foo.bar').baz.w00fz);