javascript
  • scope
  • 2010-09-11 32 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();​ 
    
    相關問題