javascript 是否可以从函数外部访问函数局部变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9918658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to access a function local variable from outside of the function?
提问by Moon
I heard today that "it is possible to access a local variable of a function since everything in javascript is global".
我今天听说“可以访问函数的局部变量,因为 javascript 中的所有内容都是全局的”。
As far as I know, you can't access a local variable from outside of the scope of the variable.
据我所知,您不能从变量范围之外访问局部变量。
For example,
例如,
function f()
{
var myvar = "something";
}
myvar = "c"; // i'm not accessing myvar in f();
I also heard that it's possible to use for(var i in window)to access myvar. I want to confirm it is not possible since I'm not the author of the language.
我还听说可以使用for(var i in window)访问 myvar。我想确认这是不可能的,因为我不是该语言的作者。
Updated:
更新:
I asked him a code snippet, and here's what I have received.
我问了他一个代码片段,这是我收到的。
var person = {
whoIs : function()
{
var name = "name";
return name;
}
};
var str = "TEST:\n";
for(var n in person)
{
str += n;
str += " = [" + person[n] + "]\n";
}
// perform regular exp. to get the value of name variable.
alert(str);
It's not accessing the variable.........it's simply printing how the function looks like...
它不是访问变量......它只是打印函数的样子......
回答by Corbin
That developer was wrong. Those two myvarare different. The outside one is equivalent to window.myvar, but the inside one is only inside the f.
那个开发商错了。那两个myvar是不同的。外面的相当于 window.myvar,但里面的只是在f里面。
Edit: a very simple example: http://jsfiddle.net/mRkX3/
编辑:一个非常简单的例子:http: //jsfiddle.net/mRkX3/
Edit 2:
编辑2:
A quote from the ECMAScript standard:
引用 ECMAScript 标准:
If the variable statement occurs inside a FunctionDeclaration, the variables are defined with function-local scope in that function, as described in section 10.1.3. Otherwise, they are defined with global scope (that is, they are created as members of the global object, as described in section 10.1.3) using property attributes { DontDelete }. Variables are created when the execution scope is entered. A Block does not define a new execution scope. Only Program and FunctionDeclaration produce a new scope. Variables are initialised to undefined when created. A variable with an Initialiser is assigned the value of its AssignmentExpression when the VariableStatement is executed, not when the variable is created.
如果变量语句出现在 FunctionDeclaration 中,则变量在该函数中定义为函数局部作用域,如第 10.1.3 节所述。否则,它们是在全局范围内定义的(即,它们被创建为全局对象的成员,如第 10.1.3 节所述)使用属性属性 { DontDelete }。在进入执行范围时创建变量。Block 没有定义新的执行范围。只有 Program 和 FunctionDeclaration 会产生新的作用域。变量在创建时初始化为 undefined。具有 Initialiser 的变量在执行 VariableStatement 时被分配其 AssignmentExpression 的值,而不是在创建变量时。
Found through http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoistingthough that article is referencing a deadlink (live link: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf).
通过http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting找到,尽管该文章引用了一个死链接(实时链接:http: //www.ecma-international.org/publications/files/ ECMA-ST/Ecma-262.pdf)。
回答by crinklywrappr
Sounds like a communication breakdown. I'm betting he was trying to make the point that you can define a global variable from inside a function by attaching it to the window object. Try:
听起来像是沟通障碍。我敢打赌,他试图说明您可以通过将全局变量附加到 window 对象来从函数内部定义全局变量。尝试:
function f()
{
window.myvar = "something";
}
myvar = "c"; // overwriting "something"
回答by u283863
In your example:
在你的例子中:
function f(){
var myvar = "something";
}
myvar = "c"; //nope, they are different.
myvar = "c"
is referring to window.myvar
, which is not the same as the myvar
inside f()
. You can not access the variables inside the function unless it is declared outside it.
myvar = "c"
指的是window.myvar
,这是不一样的myvar
内部f()
。除非在函数外部声明,否则您无法访问函数内部的变量。
var myvar;
function f(){
myvar = "something";
}
myvar = "c"; //now it changes "something" to "c"!
回答by brezanac
Judging by the example he used (for(var i in window)
) he is clearly counting on your function variables to be declared as global (without var
proceeding them).
从他使用的示例 ( for(var i in window)
)来看,他显然指望将您的函数变量声明为全局变量(而不var
继续执行它们)。
Since in that case all of the defined variables are bound for the window
object it would be easy to access them by simply traversing the window object. However, if you use var
to declare local function variables they will be available only to the function itself (window
object won't contain them).
由于在这种情况下所有定义的变量都绑定到window
对象,因此只需遍历 window 对象就可以轻松访问它们。但是,如果您使用var
声明局部函数变量,它们将仅可用于函数本身(window
对象将不包含它们)。
回答by Epuri
Yes, you are right about it. Variables declared within function scope will not be accessible out side the function. If function returns myvar
then it will be possible access its value but not the variable directly.
是的,你是对的。在函数范围内声明的变量将无法在函数外访问。如果函数返回,myvar
则可以访问其值,但不能直接访问变量。
回答by helly0d
in your case myVar
is in the scope of the function and if it is defined there with var
it will never be accessible to the outside, but if it is not defined than you are setting myVar to the global object ( window ) and it could lead to lots of trouble.
在你的情况下 myVar
是在函数的范围内,如果它在那里定义,var
它永远不会被外部访问,但如果它没有定义,那么你将 myVar 设置为全局对象( window ),它可能会导致很多的麻烦。
function(f) {
myVar = "hello"
}
In this case you can find myVar
, because it is not defined inside the function.
在这种情况下,您可以找到myVar
,因为它没有在函数内部定义。
回答by Bhavya Anand
function f()
{
var myvar = "Moon";
}
f();
myvar = "Sunlight";