TestOut

by Mabuti

JavaScript

// Creating an extend function to extend a 
// namespace with one line of code. I will admit 
// that I stole this from out on the nets. So I
// have added my comments.
//
// Variables: 
//    ns -------- Parent namespace
//    ns_string - New nested namespaces
function extend( ns, ns_string ) {
    
    // Split the namespace string that was passed 
    // in to separate out the new nested namespaces.
    var ns_splits = ns_string.split('.'),
        // The parent namespace that was passed in.
        parent = ns,
        // The iteration variable.
        i;
    
    // Check if the parent is the initial namespace 
    // in the namespace string. If so we strip it out.
    if (ns_splits[0] == 'TestOut') {
        ns_splits = ns_splits.slice(1);
    }
  
    // Loop through the split array.
    for (i = 0; i < ns_splits.length; i++) {
        
        // Get the current split namespace.
        var splitns = ns_splits[i];
        
        // Check if the parent already has the split namespace
        // if it isn't, then create it.
        if (typeof parent[splitns] == 'undefined') {
            parent[splitns] = {};
        }
        
        // Assign the parent to reference the 
        // deepest namespace.
        parent = parent[ns_splits[i]];
    }
    // Return the newly constructed namespace.
    return parent;
};

// Safely create the TestOut namespace.
// This can be shortened with:
//     var TestOut = TestOut || {};
//-----------------------------------
if (typeof TestOut === 'undefined') {
    var TestOut = {};
    
    // Create the sub-namespace for enums
    extend(TestOut,'TestOut.enums');
}

// Create a enum for sex
TestOut.enums.sex = {
    M: 'Male',
    F: 'Female'
}

// Create a enum for haircolor
TestOut.enums.haircolor = {
    BLONDE: 'Blonde',
    BROWN: 'Brown',
    BLACK: 'Black',
    RED: 'Red',
    NONE: 'No Hair'
}

// Define the Person class within the TestOut namespace
TestOut.Person =...