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
1 |
HandyLibrary.foo(); |
1 |
var p = HandyLibrary.property; |
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
1 |
Foo.someFunction() |
1 |
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.
1 2 3 4 5 6 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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; })(); |
1 2 3 4 5 6 7 8 9 10 11 12 |
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"); } |
1 2 3 4 5 |
[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 |
Why not join our forum, follow the blog and or follow me on Twitter for more informations and updates