Javascript沙盒模式示例实现

Posted

技术标签:

【中文标题】Javascript沙盒模式示例实现【英文标题】:Javascript Sandbox Pattern example implementation 【发布时间】:2012-06-26 14:31:16 【问题描述】:

在 Stoyan Stefanov 的巨著“javascript 模式”的第 101 页上,他解释了沙盒模式。 我非常喜欢他的书,但我真的在这里错过了一些现实生活中的例子,以便更好地理解他所说的内容。 Like the sandbox pattern!

我正在寻找一个现实生活中的工作实施,比如复制和粘贴起点,只是一个简单的例子,可以完全理解它。

有吗?

【问题讨论】:

看看requirejs。 require 函数的工作方式与沙盒模式非常相似,只是添加了延迟模块加载的附加功能。 【参考方案1】:

所以我尝试并想出了这个解决方案:

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] && "string" === typeof args[0]) ? 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: "Sandbox",
        version: "1.0",
        getName: function() 
            return this.name;
        
    
;

Sandbox.modules = ;

Sandbox.modules.color = function (box) 
    // private
    var initialColor = $('#main').css('color');    

    // set a red color
    box.setMainRed = function() 
        $('#main').css('color','red');
        return false;
    ,

    // get the current color
    box.getInitialColor = function () 
        return initialColor;
    ;


// another module
Sandbox.modules.style = function (box) 
    // set a red color
    box.setStyle = function() 
        $('#main').css('font-style','italic');
        return false;
    ;


// page ready
$.ready(
    Sandbox(['color', 'style'], function (box) 
        console.log(box);
        box.setMainRed();
        box.setStyle();
        console.log('try access initialColor: ', box.initialColor);
        console.log('get initial color: ', box.getInitialColor());
    )
);

但我真的不确定这是否是我应该做的。 特别是添加“模块”有点令人困惑。同样在本书的前面,他使用命名空间模式来完成这项任务,但不是在这里。为什么?你也不能在这里做吗? 但是我没能把这两种模式结合起来。

受本书启发的命名空间模式示例:

var APP = APP || ;

// namespace function
APP.namespace = function (nsString) 
    var parts = nsString.split('.'),
            parent = APP,
            i;

    // strip redundant leading global
    if ("APP" === parts[0]) 
        parts = parts.slice(1);
    

    for (i = 0; i < parts.length; i += 1) 
        // create a property if it doesn't exist
        if ("undefined" === typeof parent[parts[i]]) 
            parent[parts[i]] = ;
        
        parent = parent[parts[i]];
    
    return parent;


// constructors
APP.namespace('modules.Color');

// immediate function
APP.modules.Color = (function () 
    var currentColor = $('#main').css('color'),    
    // set a red color
    setMainRed = function() 
        $('#main').css('color','red');
        return false;
    ,
    // get the current color
    getCurrentColor = function () 
        return currentColor;
    ;

    // revealing module pattern
    return 
        setMainRed: setMainRed,
        getCurrentColor: getCurrentColor
    ;
());

var doSomething = function () 
    var color = APP.modules.Color;

    color.setMainRed();
    console.log(color.currentColor);
    console.log(color.getCurrentColor());

    return false;


// page ready
$.ready(
    doSomething()
);

【讨论】:

沙盒模式是 MyApp.Utilities.Serialize.int32 等长命名空间的替代方案。这也有助于创建独立的环境。【参考方案2】:

Stoyan Stefanov 在同一章中提到了YUI version 3 implements the Sandbox pattern。 The YUI add method (API) 注册模块,the use method (API) 在沙箱实例中加载指定的模块。 API 文档中有指向源 js 文件的链接。 几乎所有 YUI 代码examples 都使用这种模式来处理 YUI 库。很少需要定义模块 - YUI 有许多核心模块,并且有一个 page 用于社区添加的自定义模块。

【讨论】:

【参考方案3】:

我已经简化了 Stoyan 的示例,以便更容易理解发生了什么。我也对它进行了更彻底的评论。

/*First define the modules of the sandbox.  These will be defined 
as properties on the constructor function because this is a 
convenient place to keep them.*/

Sandbox.modules = ;

Sandbox.modules.returnNumbers = function(MYAPP) 
    MYAPP.return100 = function() return 100;;
;

Sandbox.modules.returnLetters = function(MYAPP) 
    MYAPP.returnABC = function() return "ABC";;
;


function Sandbox() 

    /* Because Sandbox is a constructor, an new object is automatically 
    created.  Because we're in the constructor, we refer to this new object 
    as 'this'. 

    A constructor would typically be used as part of an assignment, e.g. 
    myObject = new Sandbox().  

    However, it's also legitimate javascript to use a constructor without 
    the assignment by just writing new Sandbox() with no assignment.  The 
    constructor does return an object, it's just that it doesn't get 
    assigned to anything so  is discarded.

    We're going to add functionality (methods) to the 'this' object, but 
    rather than returning it, we will pass it to the callback function, so 
    the methods can be used immediately.
    */

    var args = Array.prototype.slice.call(arguments);  //Put the arguments 
    //of the call to the Sandbox constructor in an array called args.

    var callback = args.pop(); //The last argument is the callback
    var requiredmodules = args;  //The remaining arguments are the require
    // modules

    //For each of the modules in 'requiredmodules', add the module's 
    //methods to 'this'
    for (i=0; i< requiredmodules.length; i++) 
        Sandbox.modules[requiredmodules[i]](this);
    


    //'this' now has methods returnNumbers and returnLetters

    //Call the callback.  In the example below, 'this' will be called 
    //MYAPP, which within the callback will have all the methods from 
    //the required modules.

    callback(this);





//Finally here is an example of usage

new Sandbox('returnNumbers', 'returnLetters', function (MYAPP) 

    console.log(MYAPP.return100());
    console.log(MYAPP.returnABC());
);

【讨论】:

【参考方案4】:

这里是详细的 cmets 示例:

    (function() 

/* function constructor */
function Sandbox()
    //Change arguments to array, as you know 'arguments' are not a true JS array
    //Array.prototype.slice will provide shallow copy of 'arguments'
    var args = Array.prototype.slice.call(arguments),
        //remove last element from array and return it to caller
        //our last argument is callback
        callback = args.pop(), 
        //We can pass modules as strings or as array
        //if first element is a string, take all arguemnts
        //otherwise take one element (array)
        modules = (args[0] && typeof args[0] === "string") ? args : args[0],
        modulesLength = modules.length,
        i;

    //handle calling function constructor without 'new' keyword
    if(!(this instanceof Sandbox))
        //Invoke me again!
        return new Sandbox(modules, callback);
    

    //we can add properties to 'this'
    this.someProp = "Initialized property";

    //Initialize all required modules
    for(i = 0; i < modulesLength ; i++)
        //pass reference to 'this' for each required module and invoke it
        //'this' is poiting to new object which was created 
        //after calling new Sandbox()
        Sandbox.modules[modules[i]](this);
    

    //Invoke callback and pass 'this'
    //now 'this' cotains all methods and properties
    //attached in modules functions
    callback(this);
;

//We can optionally create Sandbox methods
Sandbox.prototype = 
    version: "1.0.1",
    createdAt: new Date()
;

/* function as a first class object - saving all modules*/
Sandbox.modules = ; 

/*Create $http,$scope and $ajax modules */
/*We need box object to add new functionality*/
/*We are creating new methods by attatching them to box obect*/
/*box is a reference to 'this' called as initializator from function constructor*/
Sandbox.modules.$http = function(box)

    box.get = function()
        console.log("$http.get");
    ;

    box.post = function()
        console.log("$http.post");
    ;

    box.prop = "I'm $http property";
;

Sandbox.modules.$scope = function(box)

    box.inject = function(param1, param2)
        console.log("$scope.inject: " + param1 + " " + param2);
    ;

    box.destroy = function(o)
        console.log("$scope.destroy: " + o + " has been destroyed!");
    ;

;

Sandbox.modules.$ajax = function(box) 
    box.call = function()
        console.log("$ajax.call");
    ;     
;

//Sandbox without calling 'new' was handled in function constructor
//We are requesting for 2 modules: $scope and $http
//callback function is our new playground
//box object has $scope and $http methods and properties inside, we are ready to go!
Sandbox(["$scope", '$http'], function(box) 
    console.log(box); //contains methods from $scope and $http
    console.log(box.inject("John", "Doe"));
    console.log(box.post());

    //we can event nest our playgrounds
    Sandbox(["$ajax"], function(box)
        console.log(box); //contains only $ajax methods and properties
        console.log(box.call());

        //we can't invoke $scope or $http functions here
    );

    //we can't invoke $ajax functions here
);
)();

JSFiddle 链接:http://jsfiddle.net/Lodse4hj/

【讨论】:

以上是关于Javascript沙盒模式示例实现的主要内容,如果未能解决你的问题,请参考以下文章

PayPal sdk 不进入沙盒模式

严格模式

推送通知在沙盒模式下不起作用

PayPal REST API (PHP SDK) 成功交易未在沙盒模式下显示,在实时模式下显示

游戏编程模式-子类沙盒

如何使用 java 和 paypal api 在沙盒模式下进行大规模支付?