Javascript 沙箱模式示例实现

2024-05-07

在 Stoyan Stefanov 的伟大著作“JavaScript Patterns”的第 101 页中,他解释了沙箱模式。 我非常喜欢他的书,但我真的错过了一些现实生活中的例子,然后更好地理解他所谈论的内容。

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

有没有?


我简化了 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());
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Javascript 沙箱模式示例实现 的相关文章

随机推荐

  • 获取小部件的背景颜色 - 真的

    我无法获取小部件的实际背景颜色 在我的特殊情况下 我在使用 QTabWidget 中的小部件时遇到问题 这是在Windows7上 因此 经典的小部件有一些灰色背景 而选项卡内的小部件通常用白色背景绘制 I tried def bgcolor
  • python seaborn:按色调显示 alpha

    在seaborn中 色调为组设置不同的颜色 我可以设置吗alpha取决于组中的JointGrid 或者甚至在单个数据点上 sns set theme jg sns JointGrid data df sns x x y y hue hue
  • C# 是否可以中断 ThreadPool 内的特定线程?

    假设我已将一个工作项排入队列ThreadPool 但是如果没有要处理的数据 从BlockingQueue 如果队列为空并且队列中不再有工作 那么我必须调用Thread Interrupt方法 如果我想中断阻塞任务 但是如何用 a 做同样的事
  • WebFlux 应用程序中的 WebFilter

    我有一个使用 Spring Boot 2 0 0 M5 2 0 0 BUILD SNAPSHOT 的 Spring Boot WebFlux 应用程序 我需要将跟踪 ID 添加到所有日志中 为了让它在 WebFlux 应用程序中工作 我尝试
  • SQL - 用 varchar 替换 is null 整数

    我正在尝试用新的列替换列varchar如果 select 语句中存在空值 则为字符串 personid ISNULL personid no person 我不想更新它 只是在查询结果中将值显示为 无人 但我收到一条错误消息 将 varch
  • json_encode() 非 utf-8 字符串?

    所以我有一个字符串数组 并且所有字符串都使用系统默认值ANSI编码并从 SQL 数据库中提取 因此有 256 种不同的可能的字符字节值 单字节编码 有什么方法可以让我得到json encode 工作并显示这些字符而不必使用utf8 enco
  • VoiceOver 的 UISlider 可访问性特征?

    使 UISlider 在启用 VoiceOver 的情况下可用所需的正确 UIAccessibility 特征和处理程序是什么 是否有关于用户如何使用启用 VoiceOver 的 UISlider 的描述 以下是我最终添加到 UISlide
  • 使用 TextBox 过滤 Datagridview 行

    我有一个绑定的 datagridView 我想使用 TextBox 值对其进行过滤 我使用了这段代码 private void ChercheStextBox TextChanged object sender EventArgs e tr
  • 在php中如何设置数组的大小?

    我只想在 php 中设置数组的大小 而不必用任何值填充它 我怎么做 Use 固定阵列 http php net SplFixedArray对于固定大小的数组 array new SplFixedArray 3 array 0 1 array
  • 在 C# 中更改 Excel 单元格格式

    如何使用 C 中的 Microsoft Excel 12 0 库更改 Excel 中单元格的格式 更具体地说 我想将给定单元格更改为文本格式 我读过了 net c 改变Excel单元格格式 https stackoverflow com q
  • 向 UIControls 添加属性而不使用子类化

    我已将 UIButtons 嵌入到 TableViewCells 中 为了跟踪按钮属于哪个单元格 我想向 UIButton 添加 NSIndexPath 属性 我不想子类化 UIButton 有没有办法可以通过类别来做到这一点 编辑 我相信
  • 使用循环计算 Python 字典中元素的有效方法

    我有一个值列表 我希望在循环期间计算每个类的元素数量 即 1 2 3 4 5 mylist 1 1 1 1 1 1 2 3 2 2 2 2 3 3 4 5 5 5 5 mydict dict for index in mylist mydi
  • 当 tableView 向下滑动时显示 UISearchController

    我通过 UISearchController 在我的测试应用程序中实现了搜索栏 当我启动应用程序时 我会在导航控制器下方看到搜索栏 但如何在应用程序启动时隐藏它并仅在下拉表格视图时显示它 并在拉出表格视图时再次隐藏 我在google或you
  • 纹理不适用于网格 - OpenGL

    我正在使用 OpenGL Es 我已成功加载 obj 文件 网格 并且显示良好 但当我应用纹理时 它不显示 我添加了下面的代码 public void draw GL10 gl bind the previously generated t
  • 复制到其他计算机时无法在 WcfTestClient 中添加服务

    我想在另一台计算机上运行 WcfTestClient VS2012 中包含的一个 而不安装 VS2012 这可能吗 在我已经安装了 NET 4 5 的机器上 但是当我尝试添加 Web 服务时 它给了我以下堆栈跟踪 Exception Tex
  • 是否可以仅使用密码进行身份验证,即使设备在 ios、swift 中具有 touch id 功能

    我要认证only使用PassCode甚至设备有Touch ID特征 我在用着 deviceOwnerAuthentication评估政策方法 当我使用这个时 如果用户已注册触摸 ID gt 始终要求提供触摸 ID 如果用户尚未注册 touc
  • Django- CMS:占位符中的插件位置

    我正在寻找一种方法来检查插件在 Django CMS 占位符中的位置 我发现这个问题检测占位符中的最后一个插件 https stackoverflow com questions 5543947 detect last plugin in
  • 将元组划分为多个元组的类型安全方法

    我们有一个特征 除其他外 还包含execute T lt Record Seq Session gt T Seq T 方法 其中Record是我们从数据库中检索的所有特征的超级特征 trait DbTrait val threadCount
  • 如何将Windows服务中的参数从Installer传递到Program.cs中的Main函数?

    我已成功将参数从 Installutil 传递到我的 serviceinstaller 但我似乎无法将这些参数传递到 Main string args 函数 这就是我尝试做到这一点的方法 如果有更好的方法来做我正在做的事情请告诉我 prot
  • Javascript 沙箱模式示例实现

    在 Stoyan Stefanov 的伟大著作 JavaScript Patterns 的第 101 页中 他解释了沙箱模式 我非常喜欢他的书 但我真的错过了一些现实生活中的例子 然后更好地理解他所谈论的内容 我正在寻找一个现实生活中的工作