JSFiddle - React, Tailwind, and code Playground
by Mabuti
JavaScript
// Safely create the namespace.
// This can be shortened with:
// var Homework = Homework || {};
//-----------------------------------
if (typeof Homework == "undefined") {
var Homework = {};
};
// Creating an extend function to extend the
// 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.
var parent = ns;
// The iteration variable.
var i;
// Check if the parent is the initial namespace
// in the namespace string. If so we strip it out.
if (ns_splits[0] == parent) {
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;
};
/*
// Define the Person constructor
var Person = function(firstName,lastName,age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.hairColor =
};
// Add a couple of methods to Person.prototype
Person.prototype.myName = function(){
console.log("My name is " + this.firstName + " " + this.lastName);
};
Person.prototype.myAge = function(){
console.log("My age is " + this.age);
};
// Enum...