标签:style blog http io 使用 java ar strong for
沙盘模式可以弥补命名空间模式中的两项不足之处:
new Sandbox(function (box) { // your code here... });
Sandbox([‘ajax‘, ‘event‘], function (box) { // console.log(box); });
Sandbox(‘ajax‘, ‘dom‘, function (box) { // console.log(box); });
Sandbox(‘*‘, function (box) { // console.log(box); }); Sandbox(function (box) { // console.log(box); });
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 });
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 () {}; };
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; } };
标签:style blog http io 使用 java ar strong for
原文地址:http://www.cnblogs.com/Bryran/p/3976146.html