Last active
March 19, 2019 15:43
-
-
Save thadeu/fa67fc60aa40197cf19a109020206a05 to your computer and use it in GitHub Desktop.
JS: 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
Sandbox.modules = {} | |
Sandbox.modules.vnode = function(s) { | |
s.vnode = {} | |
} | |
Sandbox.modules.createElement = function(s) { | |
s.createElement = function() {} | |
} | |
function Sandbox() { | |
const args = Array.prototype.slice.call(arguments) | |
let callback = args.pop() | |
let modules = args[0] && typeof args[0] === 'string' ? args : args[0] | |
function ownProperty(prop) { | |
return Sandbox.modules.hasOwnProperty(prop) | |
} | |
if (!this instanceof Sandbox) { | |
return new Sandbox(modules, callback) | |
} | |
if (!modules || modules[0] === '*') { | |
modules = [] | |
for (let i in Sandbox.modules) { | |
if (ownProperty(i)) { | |
modules.push(i) | |
} | |
} | |
} | |
for (let i in modules) { | |
if (ownProperty(modules[i])) { | |
Sandbox.modules[modules[i]](this) | |
} | |
} | |
callback && callback(this) | |
} | |
new Sandbox(['*'], function($scope) { | |
console.log($scope) // -> Sandbox {vnode: {…}, createElement: ƒ}createElement: ƒ ()vnode: {}__proto__: Object} | |
}) | |
new Sandbox(['createElement'], function($scope) { | |
console.log($scope) // -> Sandbox {createElement: ƒ}createElement: ƒ ()vnode: {}__proto__: Object} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment