Skip to content

Instantly share code, notes, and snippets.

@Williammer
Created June 6, 2014 01:45
Show Gist options
  • Select an option

  • Save Williammer/18380c1128468f63b9b5 to your computer and use it in GitHub Desktop.

Select an option

Save Williammer/18380c1128468f63b9b5 to your computer and use it in GitHub Desktop.
jsPatterns.sandbox.js
function Sandbox() {
// turning arguments into an array
var args = Array.prototype.slice.call(arguments),
// the 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;
// make sure the function is called // as a constructor
if (!(this instanceof Sandbox)) {
return new Sandbox(modules, callback);
}
// add properties to `this` as needed:
this.a = 1;
this.b = 2;
// now 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);
}
}
}
// initialize the required modules
for (i = 0; i < modules.length; i += 1) {
Sandbox.modules[modules[i]](this);
}
// call the callback
callback(this);
}
// any prototype properties as needed
Sandbox.prototype = {
name: "My Application",
version: "1.0",
getName: function () {
return this.name;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment