JSFiddle - React, Tailwind, and code Playground

by megsuma

JavaScript

// Define a base Namespace
if( false === window.hasOwnProperty( 'WDS ') ) {
    window.WDS = {};
}

WDS.Namespace = function( ns ) {
    var args = ns.split( '.' );
    var cur;

    for( var i = 0; i < args.length; i++ ) {
        if( 0 === i ) {
            window[ args[ i ] ] = window[ args[ i ] ] || {};
        }
        
        cur = cur || window[ args[ i ] ];
        
        if( 0 < i ) {
            cur[ args[ i ] ] = cur[ args [ i ] ] || {};
            cur = cur[ args[ i ] ];
        } 
    }
    
    return window[ args[ 0 ] ];
}

// Try it out on the same namespace
WDS.Namespace( 'WDS.util.Ajax' );
WDS.Namespace( 'WDS.util.mobile.SrollWatcher' );

console.log( WDS );

WDS.Namespace( 'Something.Totally.New' );

console.log( Something );

// Now we can interact with the structure
WDS.util.Ajax.makeCall = function( url, data ) { /* Do something Ajax-y */ }
Something.Totally.name = 'Bar';
Something.WhoAmI = function() { console.log( this.Totally.name ); }

Something.WhoAmI(); // Bar