Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rudimusmaximus/bbfe6a0bba91de6364e7279e9b68853b to your computer and use it in GitHub Desktop.
Save rudimusmaximus/bbfe6a0bba91de6364e7279e9b68853b to your computer and use it in GitHub Desktop.
Completed example .gs to demonstrate 4 level namespacing
/****
* 'SEED' THE NAMESPACES
*****/
var NAME$PACE1 = (function(ns) {
ns.author = "Raul Flores, Jr";
ns.description = "This is an example 4 level namespace."
ns.value = "some value";
ns.Enums = {
ZERO: 0,
ONE: 1,
TWO: 2
};
ns.doSomething = function () {
return 100;
};
return ns;
})(NAME$PACE1 || {});
/****
* 'POPULATE' THE NAMESPACE - define additional methods and properties
* Nested level functions
* to 'nest' a nameNAME$PACE use the pattern, just declare and pass the nested object when defining the immediately invoked function
*****/
(function (nested) {
/**
* notes on what was done
* @return {string} returns statement in string
*/
nested.subsetOfFunctions1.doSomething = function () {
return "This is the something that was done.";
};
return nested;
})(NAME$PACE1.GroupingA = {subsetOfFunctions1:{}});
function testIt(){
var x = NAME$PACE1.GroupingA.subsetOfFunctions1.doSomething();
Logger.log(x);
Logger.log(JSON.stringify(NAME$PACE1));
}
@rudimusmaximus
Copy link
Author

//GOT IT :) !!!
var NAME$PACE1 = (function(ns) {
  
  ns.value = "some value";
  
  ns.doSomething = function () {
     return 100;
   };
  
  return ns;
})(NAME$PACE1 || {});

(function (nested) {
  /**
  * notes on what was done
  */
  nested.subsetOfFunctions1.doSomething = function () {
    return "This is the something that was done.";
  };
  return nested;
})(NAME$PACE1.GroupingA = {subsetOfFunctions1:{}});


function testIt(){
  var x = NAME$PACE1.GroupingA.subsetOfFunctions1.doSomething();
  Logger.log(x);
  Logger.log(JSON.stringify(NAME$PACE1));
 
}

REWRITING FOR MY OWN purposes.

@rudimusmaximus
Copy link
Author

Done. see final revised gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment