Created
February 16, 2013 12:38
-
-
Save Sljubura/4966747 to your computer and use it in GitHub Desktop.
Sandbox pattern.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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