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

Notes from Bruce McPherson on google+
"There are many patterns for namespace definition, but I find the simplest pattern is to use an ief (immediately executing function)

var Space = (function(ns) {

  ns.value = "some value";

   ns.doSomething = function () {
     return 100;
   };

   return ns;
})(Space || {});

And use it like this

var x = Space.value;
Space.doSomething();

if you wanted to 'nest' a namespace, then it can use the same pattern.


(function (nested) {
  nested.inner = function () {
    return 200;
  };
  return nested;
})(Space.Nested = {});

then Space.Nested.inner();

In fact when you create something in the global space, you are already in a namespace, although its implied .. so

(function (w) {
  w.f = function (){};
})(this);

is the same as, in the global space
function f();

From that you can see how you could have defined nested externally and passed Space to it as its parent.

Note that namespaces are not the same thing as objects created by a constructor pattern and the new keyword. There is only one instance of a namespace, there can be many instances using the new keyword.

There are other approaches too, such as using Object.create and closures.

See here for a discussion on the prototype chain
ramblings.mcpher.com - Using Object.create in Apps Script - Desktop Liberation

I recommend you get some more practice in the various techniques before diving into nested namespaces - you may not really need them.
"

@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