Namespaces in libraries and scripts

As you know, in Apps Script you can have libraries that can be accessed from other scripts. Apps Script automatically generates a namespace for that library (it calls it an identifier) which you prefix calls to functions to that library with. 

Let's say you have a library called HandyLibrary, and you've included it as a resource, you can access functions in it like this

HandyLibrary.foo();

and variables in it like this

var p = HandyLibrary.property;

If you've implemented jsdoc in your library, you also get some limited autocomplete tips in the Apps Script editor when you save a version.

One useful benefit of namespaces is that you can limit the risk of global  name collision (multiple scripts declaring the same global variable or function name for different things), since multiple namespaces can have the same locally named function - so

Foo.someFunction()

is not the same as

Foo2.someFunction()


Local namespaces

If you are developing a large project with multiple script files, and especially if you are copying code from existing projects, there's a chance that you'll waste a lot of time tracking down this kind of name collision problem. I recommend that you implement local namespaces in each of your script files to avoid this. 

Here's how - Let's say you create a script file called Foo.

Create a name space - like this.

var Foo = (function() {
    'use strict';
    // this is namespace object
    var foo = {};

    return foo;
})();

Add your variables and functions and functions that you would normally have declared globally inside your namespace
var Foo = (function() {
    'use strict';
    // this is namespace object
    var foo = {};

    // set a local property
    foo.name = 'foo';

    // a function in this namespace
    foo.showName = function () {
        Logger.log('namespace ' + foo.name);
    };

    return foo;
})();

You can add pseudo classes inside that too

var Foo = (function() {
    'use strict';
    // this is namespace object
    var foo = {};

    // set a local property
    foo.name = 'foo';

    // a function in this namespace
    foo.showName = function () {
        Logger.log('namespace ' + foo.name);
    };

    // a pseudo class in this name space
    foo.speak = function (name) {

    // self will refer to this instance
        var self = this;

        // a local param
        var name_ = name;

        // a method
        self.sayHello = function (message) {
            Logger.log('hello from ' +name_ + ' ' + message);
        };

        // another method
        self.sayGoodbye = function (message) {
            Logger.log('bye from ' +name_ + ' ' + message);
        };
    }

    return foo;
})();


Use it just like a library from other script files inside the same project. 

function test() {

    // access a local function in the namespace
    Foo.showName();


    // a couple of instances

    var tom = new Foo.speak ('tom');
    var jerry = new Foo.speak ('jerry');


    tom.sayHello("cat");
    jerry.sayHello("mouse");


    tom.sayGoodbye("cat");
    jerry.sayGoodbye("mouse");


}


and the result
[15-05-13 09:16:31:498 BST] namespace foo
[15-05-13 09:16:31:499 BST] hello from tom cat
[15-05-13 09:16:31:499 BST] hello from jerry mouse
[15-05-13 09:16:31:499 BST] bye from tom cat
[15-05-13 09:16:31:500 BST] bye from jerry mouse

For more like this, see  Google Apps Scripts snippets. Why not join our cummunity , follow the blogtwitterG+  .

You want to learn Google Apps Script?

Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly

If you prefer Video style learning I also have two courses available. also published by O'Reilly.
Google Apps Script for Developers and Google Apps Script for Beginners.



Comments