繁体   English   中英

JavaScript 中的独立执行上下文

[英]Isolated execution context in JavaScript

我正在尝试在 JavaScript 中的空隔离执行上下文中执行一段代码。 在下面的示例中,我正在尝试隔离isolated执行范围。 我想要做的是在没有全局变量的上下文中执行一个函数。

(function() {
  'use strict';

  var scope = Object.create(null);
  var isolated = function() {
    'use strict';
    console.log(document); // Trying to get undefined
                           // but traces `document`.
  };

  isolated.call(scope);
})();

我认为取消全局变量很简单,但是太多了!

var isolated = function(window, document, location /* etc */) {
  // ...
};

isolated.call(scope, undefined, undefined, undefined /* etc */);

有一个更好的方法吗?

在 javascript 本身中没有好的方法可以做到这一点(但请参阅 Gareth Hayes 的回答以了解另一种选择)。

有几种不好的方法。

(function() {
  var scope = Object.create(null);
  var obscurer = {};
  for (var key in this) {
     obscurer[key] = undefined;
  }

  with (obscurer) {
    var isolated = function() {
      'use strict';
      console.log(document);
    };
  }

  isolated.call(scope);
})();

请注意,您实际上会收到一个错误,因为没有定义控制台而不是文档,尽管您可以通过不阻止模糊对象中的“控制台”来解决此问题。 您可能会发现您需要比您意识到的更多的全局变量。

您也只是阻止了window.enumerable 的可枚举属性。 如果您也意识到要阻止的不可数属性,则必须将它们添加到模糊器中。

当然,使用with意味着你也不能再使用严格模式了,每个人都会看不起你。

如果您在节点而不是浏览器中工作,则有更多有趣的选项可用。

使用我的 MentalJS 解析器来隔离环境。 然后,您可以通过自定义代码来选择它可以访问哪些对象/变量。

http://businessinfo.co.uk/labs/MentalJS/MentalJS.html

http://code.google.com/p/mentaljs/

默认情况下,它允许访问文档,但您可以阻止这种情况,请在此处自定义环境http://code.google.com/p/mentaljs/source/browse/trunk/MentalJS/javascript/Mental.js#260然后您可以选择如果他们可以访问数学等。

它可以在没有 ECMA6 的情况下通过使用包含可信需求保护代码的 IIFE 来完成,您可以在其中注入不可信需求隔离代码(参见示例)。

(function(injectedFunction) {
    /* Trusted code, that needs protection from untrusted code access */
    var hostingFuncPrivatePrimitive = "Hello there";
    var hostingFuncPrivateObject = {
        this_is_mine: true
    };

    var sharedPrimitive = 'This is shared';
    var sharedObject = {};

    // Running the untrusted code:
    injectedFunction(sharedPrimitive, sharedObject);

    console.log("sharedObject is: " + JSON.stringify(sharedObject));
    console.log("hostingFuncPrivateObject is: " +
        JSON.stringify(hostingFuncPrivateObject));
})(

(function(primitiveArg, objArg) {
    /* Untrusted code that needs isolation */

    // 1. using primitive (legal)
    console.log('primitiveArg is: ' + primitiveArg);

    // 2. Writing value to given objArg (legal):
    objArg.mumu = 'mimi';

    // 3. Trying to access host function variables (illegal)
    try {
        console.log('hostingFuncPrivatePrimitive is:' +
            hostingFuncPrivatePrimitive);
        hostingFuncPrivateObject.this_is_mine = false;
    } catch (e) {
        console.log(e);
    }
})

);

如果您将上述内容放在 Chrome 的控制台中,您将获得:

    primitiveArg is: This is shared
    VM117:29 ReferenceError: hostingFuncPrivatePrimitive is not defined
            at <anonymous>:26:17
            at <anonymous>:11:5
            at <anonymous>:16:3
    VM117:12 sharedObject is: {"mumu":"mimi"}
    VM117:13 hostingFuncPrivateObject is: {"this_is_mine":true}

PS:我知道我参加聚会迟到了,但也许这对任何人都有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM