javascript
  • scope
  • 2010-09-11 31 views 4 likes 
    4

    我试图声明函数匿名函数之外,但仍然有接取到所有的匿名函数变量JavaScript函数改变变量的作用域

    下面是展示我在说什么。

    我只是需要摆脱eval。

    //Used to determine where the variable is being stored 
    var variableScope = "global"; 
    
    (function(window){ 
         var variableScope = 'insideFunction', 
         appearingToBeGlobalFunction = function(){ 
           alert("This Function appears Global but really isn't"); 
         }; 
         window["addFunction"]=function(funName,fun){ 
           //window[funName] = fun; Doesn't work 
           eval("window[funName]="+fun+";"); 
         } 
    })(window); 
    
    addFunction("alertTest",function(){ 
         alert(variableScope); 
         appearingToBeGlobalFunction(); 
    }); 
    
    //should alert "insideFunction" and "This Function appears Global but really isn't" 
    alertTest(); 
    

    编辑:这个问题的目标是最终保持在全球范围内提供大量的变量干净,但仍无法访问,设置和调用,好像他们是全球性的便利。我已经总结出有一种方法可以做到我之后的事情,但它需要在JavaScript中使用不推荐的功能。 下面是一些示例代码,演示如何在没有eval的情况下完成上述操作。 This article讨论如何使用“with”。

    var variableScope = "global"; 
    
    var customScope = { 
         variableScope : 'insideFunction', 
         appearingToBeGlobalFunction : function(){ 
           alert("This Function appears Global but really isn't"); 
         } 
    }; 
    
    function alertTest(){ 
         with(customScope){ 
          alert(variableScope); 
          appearingToBeGlobalFunction(); 
         } 
    }; 
    
    //should alert "insideFunction" and "This Function appears Global but really isn't" 
    alertTest();​ 
    
    +0

    你有一个局部变量名为“appearingToBeGlobalFunction”......这是一个悖论。你为什么认为分配给该局部变量的函数应该是全局的?它是本地的。 – 2010-09-11 16:39:06

    +0

    @ŠimeVidas:他没有使用'var'来定义该变量,因此它在全局上下文中定义。它应该被重命名为'appearingToBeGlobalFunctionAndActuallyIs'。 – 2010-09-11 16:49:46

    +0

    是的,他做到了。 “variableScope”和“出现......”都是局部变量。 (注意它们之间的逗号分隔符)。 http://stackoverflow.com/questions/694102/declaring-multiple-variables-in-javascript – 2010-09-11 16:58:25

    回答

    2

    你不能摆脱eval,仍然期望它的工作。这是在“关闭”之后查看范围成员的唯一方法。过去我已经搞砸了similar,但我绝对不会在任何地方使用它。考虑一个替代解决方案,无论你想完成什么。

    0

    你可能会迫使变量是在全球范围内如代替var variableScope = 'insideFunction'您使用window.variableScope = 'insideFunction'

    +0

    解决方案不应该引入更多的全局变量。 – 2010-09-11 16:34:37

    2
    eval("window[funName]="+fun+";"); 
    

    哦,亲爱的上帝。

    的原因,这个“作品”是要转换的功能funalertTest)转换为字符串把它在eval说法。

    在大多数桌面浏览器中,原生JS函数的toString()结果将是一个看起来像包含与原始声明相同代码的函数表达式的字符串。您将函数转换回字符串,并在新的封闭函数的上下文中重新解析该字符串,所以新的函数值是相同的代码,但具有不同的闭包。

    但是,并不要求Function#toString这样的工作,并且in some cases it won't。依赖函数分解是不安全的;避免。

    你当然只能使用eval做这种可怕的骇客,尽管没有理由window[funName]=部分必须在eval之内。 window[funName]= eval('('+fun+')');会同样好(非常好)。

    我试图声明函数匿名函数之外,但仍然有接取到所有的匿名函数变量

    Whyever你会做一些疯狂的喜欢吗?

    0

    这个问题的目标是最终保持在全球范围内提供大量的变量干净,但仍无法访问,设置和调用,好像他们是全球性的便利。我已经总结出有一种方法可以做到我之后的事情,但它需要在JavaScript中使用不推荐的功能。 下面是一些示例代码,演示如何在没有eval的情况下完成上述操作。 This article讨论如何使用“with”。

    var variableScope = "global"; 
    
    var customScope = { 
         variableScope : 'insideFunction', 
         appearingToBeGlobalFunction : function(){ 
           alert("This Function appears Global but really isn't"); 
         } 
    }; 
    
    function alertTest(){ 
         with(customScope){ 
          alert(variableScope); 
          appearingToBeGlobalFunction(); 
         } 
    }; 
    
    //should alert "insideFunction" and "This Function appears Global but really isn't" 
    alertTest();​ 
    
    相关问题