Created
June 6, 2014 01:45
-
-
Save Williammer/18380c1128468f63b9b5 to your computer and use it in GitHub Desktop.
jsPatterns.sandbox.js
This file contains hidden or 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), | |
| // 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