Last active
December 10, 2015 21:58
-
-
Save jcblw/4498121 to your computer and use it in GitHub Desktop.
Sandboxing pattern & some demo code
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(_export) { | |
var Sandbox = function(modules, callback) { | |
if (!(this instanceof Sandbox)) { | |
return new Sandbox(modules, callback); | |
} | |
// modules is an array in this instance: | |
for (var i = 0, len = modules.length; i < len; i++) { | |
this.Modules[modules[i]](this); | |
} | |
callback(this); | |
}; | |
Sandbox.prototype.Modules = Sandbox.modules = {}; | |
_export.Sandbox = Sandbox; | |
})(this); | |
Sandbox.modules.people = function(sandbox) { | |
sandbox.people = {}; | |
sandbox.people.all = function(){return 'list of people'}; | |
sandbox.people.me = function(){return 'logged in user'}; | |
}; | |
Sandbox.modules.pets = function(sandbox) { | |
sandbox.pets = {}; | |
sandbox.pets.dogs = function(){return 'list of dogs'}; | |
sandbox.pets.cats = function(){return'list of cats'}; | |
}; | |
Sandbox(['people', 'pets'], function(sandbox){ | |
// i can modify variables | |
sandbox.people.me = function(){return 'Jacob'}; | |
console.log(sandbox); | |
}); | |
Sandbox(['people'], function(sandbox){ | |
console.log(sandbox); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment