Skip to content

Instantly share code, notes, and snippets.

@Sljubura
Created February 16, 2013 12:38
Show Gist options
  • Save Sljubura/4966747 to your computer and use it in GitHub Desktop.
Save Sljubura/4966747 to your computer and use it in GitHub Desktop.
Sandbox pattern.
function Sandbox() {
// turning arguments into an array
var args = Array.prototype.slice.call(arguments),
// last argument is the callback
callback = args.pop(),
// modules can be passed as an array or as individual parameters
modules = (args[0] && typeof args[0] === "string") ? args : args[0],
i;
// Making the call as a constructor
if (!(this instanceof Sandbox)) {
return new Sandbox(modules, callback);
}
// Add properties to this
this.a = 1;
this.b = 2;
// add modules to the core 'this' object
// no modules or '*' both mean 'use all modules'
if (!modules || modules === '*') {
modules = [];
for (i in Sandbox.modules) {
if (Sandbox.modules.hasOwnProperty(i)) {
modules.push(i);
}
}
}
// init the required modules
for (i = 0; i < modules.length; i += 1) {
Sandbox.modules[modules[i]](this);
}
// callback
callback(this);
// prototype properties
Sandbox.prototype = {
name: 'APP',
getName: function () {
return this.name;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment