简体   繁体   English

Javascript函数更改变量范围

[英]Javascript function change variables scope

I am attempting to declare a function outside of anonymous function but still have acess to all of the anonymous functions variables 我试图在匿名函数之外声明一个函数,但仍然可以访问所有匿名函数变量

Below is demonstrating what I'm talking about. 下面是我正在谈论的内容。

I just need to get rid of eval. 我只需要摆脱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();

Edit: The goal of this question was to ultimately keep the global scope clean from tons of variables, but still have the convenience of accessing, set and calling as if they were global. 编辑:这个问题的目标是最终使全局范围从大量变量中保持清洁,但仍然具有访问,设置和调用的便利,就好像它们是全局的一样。 I have concluded there is a way to doing what I'm after but it requires a deprecated functionality in javascript. 我已经得出结论,有一种方法可以做我想要的事情,但它需要在javascript中使用已弃用的功能。 Here is some example code showing how to accomplish the above without eval. 这是一些示例代码,显示如何在没有eval的情况下完成上述操作。 This article discusses how to use "with". 本文讨论如何使用“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();​
eval("window[funName]="+fun+";");

Oh dear Lord. 亲爱的主啊。

The reason this “works” is that you are converting the function fun ( alertTest ) into a string to put it in the eval argument. 这个“有效”的原因是你将函数funalertTest )转换为字符串以将其放入eval参数中。

It happens that in most desktop browsers, a native JS function's toString() result will be a string that looks like a function expression containing the same code as the original declaration. 碰巧在大多数桌面浏览器中,本机JS函数的toString()结果将是一个看起来像函数表达式的字符串,其中包含与原始声明相同的代码。 You're turning a function back into a string and re-parsing that string in the context of the new enclosing function, so the new function value is the same code but with a different closure. 您正在将函数转换回字符串并在新的封闭函数的上下文中重新解析该字符串,因此新函数值是相同的代码但具有不同的闭包。

However, it is not required that Function#toString work like this, and in some cases it won't . 但是, Function#toString像这样工作, 在某些情况下它不会 It is not safe to rely on function decomposition; 依靠函数分解是不安全的; avoid. 避免。

You can certainly only do this kind of horrific hackery using eval , although there is no reason the window[funName]= part has to be inside the eval . 你当然只能使用eval做这种可怕的hackery,尽管没有理由window[funName]= part必须在eval window[funName]= eval('('+fun+')'); would work equally well (badly). 会很好地工作(严重)。

I am attempting to declare a function outside of anonymous function but still have acess to all of the anonymous functions variables 我试图在匿名函数之外声明一个函数,但仍然可以访问所有匿名函数变量

Whyever would you do something crazy like that? 为什么你会做那样疯狂的事情?

You can't get rid of eval and still expect it to work. 你无法摆脱eval,仍然希望它能够发挥作用。 That's the only way to take a look at members of the scope after it's been "closed." 这是在关闭范围之后查看范围成员的唯一方法。 I've messed around with something similar in the past, but I would never actually use it anywhere. 我过去曾经搞过类似的东西,但实际上我从未在任何地方使用它。 Consider an alternate solution to whatever you're trying to accomplish. 考虑一个替代解决方案,无论你想要完成什么。

你可以强制变量在全局范围内,例如代替var variableScope = 'insideFunction'你使用window.variableScope = 'insideFunction'

The goal of this question was to ultimately keep the global scope clean from tons of variables, but still have the convenience of accessing, set and calling as if they were global. 这个问题的目标是最终保持全球范围内的大量变量,但仍然具有访问,设置和调用的便利,就好像它们是全局的一样。 I have concluded there is a way to doing what I'm after but it requires a deprecated functionality in javascript. 我已经得出结论,有一种方法可以做我想要的事情,但它需要在javascript中使用已弃用的功能。 Here is some example code showing how to accomplish the above without eval. 这是一些示例代码,显示如何在没有eval的情况下完成上述操作。 This article discusses how to use "with". 本文讨论如何使用“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();​

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

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