是否可以从函数外部访问函数局部变量? [英] Is it possible to access a function local variable from outside of the function?

查看:291
本文介绍了是否可以从函数外部访问函数局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天听说因为javascript中的所有内容都是全局的,所以可以访问函数的局部变量。

I heard today that "it is possible to access a local variable of a function since everything in javascript is global".

据我所知,你可以不要从变量范围之外访问局部变量。

As far as I know, you can't access a local variable from outside of the scope of the variable.

例如,

function f()
{
    var myvar = "something";
}

myvar = "c"; // i'm not accessing myvar in f();

我还听说可以使用 for(var i in window)访问myvar。我想确认这是不可能的,因为我不是该语言的作者。

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.

更新:

我问了他一个代码片段,这就是我收到的内容。

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...

推荐答案

开发人员错了。那两个 myvar 是不同的。外面的一个等同于window.myvar,但内部的一个只在 f 中。

That developer was wrong. Those two myvar are different. The outside one is equivalent to window.myvar, but the inside one is only inside the f.

编辑:一个非常简单的例子:< a href =http://jsfiddle.net/mRkX3/ =nofollow> http://jsfiddle.net/mRkX3/

a very simple example: http://jsfiddle.net/mRkX3/

编辑2:

来自ECMAScript标准的报价:

A quote from the ECMAScript standard:


如果变量语句出现在FunctionDeclaration中,变量在该函数中使用函数局部作用域定义,如10.1.3节所述。否则,它们使用属性属性{DontDelete}定义为全局范围(即,它们是作为全局对象的成员创建的,如10.1.3节中所述)。输入执行范围时会创建变量。块不定义新的执行范围。只有Program和FunctionDeclaration产生了一个新的范围。变量在创建时初始化为未定义。执行VariableStatement时,为Initialiser的变量赋予其AssignmentExpression的值,而不是在创建变量时。

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.

通过 http://www.adequatelygood.com/2010/2/JavaScript-Scoping - 并且 - 吊装虽然该文章引用了一个死链接(实时链接: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf )。

Found through http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting though that article is referencing a deadlink (live link: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf).

这篇关于是否可以从函数外部访问函数局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆