JavaScript Patterns 5.5 Sandbox Pattern
Drawbacks of the namespacing pattern
• Reliance on a single global variable to be the application’s global. In the namespacing pattern, there is no way to have two versions of the same application or library run on the same page, because they both need the same global symbol name, for example, MYAPP.
• Long, dotted names to type and resolve at runtime, for example, MYAPP.utilities.array.
A Global Constructor
Using the sandbox will look like this:
new Sandbox(function (box) {
// your code here...
});
The object box will be like MYAPP in the namespacing example—it will have all the library functionality you need to make your code work.
Sandbox(['ajax', 'event'], function (box) {
// console.log(box);
});
module names are passed as individual argument
Sandbox('ajax', 'dom', function (box) {
// console.log(box);
});
when no modules are passed, the sandbox will assume '*'.
Sandbox('*', function (box) {
// console.log(box);
});
Sandbox(function (box) {
// console.log(box);
});
protect the global namespace by having your code wrapped into callback functions.
Sandbox('dom', 'event', function (box) {
// work with dom and event
Sandbox('ajax', function (box) {
// another sandboxed "box" object
// this "box" is not the same as
// the "box" outside this function
//...
// done with Ajax
});
// no trace of Ajax module here
});
Adding Modules
Sandbox.modules = {};
Sandbox.modules.dom = function (box) {
box.getElement = function () {};
box.getStyle = function () {};
box.foo = "bar";
};
Sandbox.modules.event = function (box) {
// access to the Sandbox prototype if needed:
// box.constructor.prototype.m = "mmm";
box.attachEvent = function () {};
box.dettachEvent = function () {};
};
Sandbox.modules.ajax = function (box) {
box.makeRequest = function () {};
box.getResponse = function () {};
};
Implementing the Constructor
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);
// Sandbox.modules[i](this);
}
}
}
// initialize the required modules (Kaibo: The code below in yellowcan be omitted by the code in yellow above to avoid the 2nd loop)
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;
}
};
References:
JavaScript Patterns - by Stoyan Stefanov (O`Reilly)
JavaScript Patterns 5.5 Sandbox Pattern的更多相关文章
- JavaScript Patterns 5.8 Chaining Pattern
Chaining Pattern - Call methods on an object one after the other without assigning the return values ...
- JavaScript Patterns 5.4 Module Pattern
MYAPP.namespace('MYAPP.utilities.array'); MYAPP.utilities.array = (function () { // dependencies var ...
- JavaScript Patterns 5.1 Namespace Pattern
global namespace object // global object var MYAPP = {}; // constructors MYAPP.Parent = function() { ...
- JavaScript Patterns 4.2 Callback Pattern
function writeCode(callback) { // do something... callback(); // ... } function introduceBugs() { // ...
- JavaScript Patterns 2.6 switch Pattern
Principle • Aligning each case with switch(an exception to the curly braces indentation rule). • Ind ...
- JavaScript Patterns 4.8 Function Properties - A Memoization Pattern
Gets a length property containing the number of arguments the function expects: function func(a, b, ...
- JavaScript Patterns 7.1 Singleton
7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...
- JavaScript Patterns 6.7 Borrowing Methods
Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...
- JavaScript Patterns 6.5 Inheritance by Copying Properties
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...
随机推荐
- GitHub简单教程
Hello World程序是计算机编程中由来已久的惯例,这是一个当你在学习新事物的时候开始的一个简单的小练习.让我们开始GitHub的"Hello World". 你将学会: - ...
- 孙鑫MFC学习笔记9:状态栏与工具栏编程
1.在窗口创建之前就应该修改窗口的样式 2.单文档应用程序会把文档名作为应用程序标题,应该去掉FWS_ADDTOTITLE属性,然后修改lpszName为标题 3.在窗口创建完成后,可以通过SetWi ...
- MySQL Workbench gnome-keyring-daemon错误的解决
在Fedora下安装了一个MySQL Workbench,运行,连接数据库,在Store in Keychain时出现了gnome-keyring-daemon错误,不能保存密码,也就连不上数据库. ...
- python验证登录
一个web2.0时代的网站,自然少不了用户注册,登录,验证的功能,那么python可以怎样实现登录验证呢 这里我们使用装饰器来做登录验证 网站构成 假设我们有这样一个网站,是一个类似与博客园这种多个用 ...
- spring入门(一)
前面介绍了spring环境的搭建,在搭建spring环境的时候分为java环境和javaWeb环境,在javaWeb环境下通常会结合springMVC使用,在java项目中只需要把spring的包导入 ...
- R语言-GA算法脚本
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...
- Mysql优化的几点总结
正常情况下,初创公司的流量并不是很大,mysql数据库在未做优化的情况依然可以满足性能要求,特别是5.6版本后mysql在性能上还是有了很大提升,所以在初期并没有花精力在此上面.但后来发生的一系列问题 ...
- windows server 2003安装sp4时的问题
"以前进行的程序安装创建了挂起的文件操作.运行程序之前,必须重新起动计算机."的解决办法 在安装SQL 2000数据库和SQLSP4补丁时,经常会提示“以前进行的程序安装创建了挂起 ...
- 设置跨域的iframe的高度
原因 如下图,A域中有个B域的页面,但是B的页面的长度不确定,A无法去设置一个准确的高度. PS:iframe高度设置auto是无效的 解决办法 如上图, (1)在B页面中加一个A的代理页面的ifra ...
- 送给我的朋友——Cry on my shoulder
If the hero never comes to you如果你的真命天子仍未来到 If you need someone you"re feeling blue如果你情绪低落需要有人陪伴 ...