Skip to content

Instantly share code, notes, and snippets.

@jcblw
Last active December 10, 2015 21:58
Show Gist options
  • Save jcblw/4498121 to your computer and use it in GitHub Desktop.
Save jcblw/4498121 to your computer and use it in GitHub Desktop.
Sandboxing pattern & some demo code
(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