示例在这里:function x(arg){ localScope[arg.name]=arg.value; alert(sex);}x({name:"sex", value:"Male"});解决方案 Okey,我发现相关问题正在谈论我的需求... 如何在javascript中动态访问本地范围? 我只记得在ECMA 262中,这只是使用"with"语句(当然还有eval)将动态局部变量动态添加到作用域的一种方法,这里是解决方法:var x=function(obj){ with(obj) { alert(someObj); } } alert(typeof someObj); x ( {someObj:"yea"}) ; alert(typeof someObj);Basically this is a question how to access local scope handler. I trying to achieve something similar for global variable definition like:window['newObject'] = "some string";alert(newObject);but for local scope. Right now only solution I have is using evals:eval("var newObject='some string'");But this is really ugly solution... The best one would be like using some reference to local scope like in a window[] solution, but I never heard of any reference to local scope... Any ideas ?Example goes here:function x(arg){ localScope[arg.name]=arg.value; alert(sex);}x({name:"sex", value:"Male"}); 解决方案 Okey I found related question that is talking about what I need...How can I access local scope dynamically in javascript?I just remember that in ECMA 262 is only one way to add dynamically local variables to scope using "with" statement (and eval of course), here are solution:var x=function(obj){ with(obj) { alert(someObj); } } alert(typeof someObj); x ( {someObj:"yea"}) ; alert(typeof someObj); 这篇关于JavaScript局部变数宣告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 11:53