防止函数在外部作用域中查找变量

Prevent function from looking for variables in outer scopes

本文关键字:查找 变量 作用域 外部 函数      更新时间:2023-09-26

我想这样做,因为我的项目通常使用如下所示的快捷方式变量:

(function() {
    var world = new World(),
        Event = world.Event,
        Guide = world.Guide;
    Event.add('updateUI', function() {
        // 1st: Correct, the value of 'this' refers to a world
        this.Guide.show('UI updated');
        // 2nd: Ok because 'world' was defined in outer scope
        world.Guide.show('UI updated');
        // 3rd: Ok because 'Guide' was defined in outer scope
        Guide.show('UI updated');
    });
})();

如果我Event.add('updateUI', function() { ... });移动到其他文件,则只有第一个语句(this.Guide.show)有效。如何防止此函数使用外部作用域的worldGuide变量,以便第二条和第三条语句从一开始就抛出错误。

use strict非常接近这一点,但它只会阻止您访问全局对象。

更新以使这个问题更清晰:主要问题是:有没有办法防止函数在外部作用域中查找变量?如果没有办法,请解释原因,因为我认为这样做是合理的。

您可以使用阴影。如果在函数内显式声明变量(而不是初始化它),则函数将使用此变量,而不是外部作用域中同名的变量。

例:

var outer = 'foo';
function isolated () {
  var outer;
  console.log(outer);
};
isolated(); // output undefined

这应该可以满足您的需求。

还有另一种方法,但这是一个丑陋的黑客,可能无法在某些浏览器(IE ?

下面是一个示例:

var outer = 'foo';
function isolated () {
  console.log(outer);
};
var e = eval;
// will throw exception: 'outer' is not defined
console.log(e('('+isolated.toString()+')()'));

evale并不完全相同。"真正的"评估可以访问整个范围,但是如果你复制它,那么javascript引擎可以通过不使外部作用域可用于评估的代码来优化它。

但是:不要那样做

  • 首先:它依赖于 function.toString() 将输出函数代码的事实。这不是规范的一部分,因此不可靠(虽然适用于 v8 和 spidermonkey)。
  • 第二:应该避免eval,它使调试成为一场噩梦(您无法访问有用的堆栈跟踪),存在性能和安全问题,并且您很快就会得到一个糟糕的代码来维护。

不知道你为什么要这样做,但使用简短的例子:

(function(theFunk) {
    var world = new World(),
        Event = world.Event,
        Guide = world.Guide;
    Event.add('updateUI',theFunk);
})(function(){
        this.Guide.show('UI updated');  // 1st: depends on what you do with it
        world.Guide.show('UI updated'); // 2nd: will fail (depends on global)
        Guide.show('UI updated');       // 3rd: will fail (depends on global)
});