调用内部函数调用外部函数中定义的变量 [英] Calling variables defined in outer function from inner function with debugger

查看:526
本文介绍了调用内部函数调用外部函数中定义的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery docs JavaScript指南



由于本地作用域通过函数进行,所以另外一个定义为
的函数可以访问外部函数中定义的变量:


< blockquote>

  function outer(){
var x = 5;
var y = 2;
function inner(){
console.log(x);
调试器; //< - !!!
}
inner();
}
outer()

使用触发的控制台调试器

 > x 
5
> y
ReferenceError:y未定义

由于外部函数可以由 inner 函数使用(例如 x y ),为什么调试器不能调用 y 变量?



我怀疑人们会回答调试器只显示在最内部/本地范围内定义的变量。原因在于,当使用内部函数中的调试器检查变量时,可以使用内部和外部范围之间的调试器进行区分。
另外,在内部范围内执行的外部范围中定义的每个变量都允许调试器访问它。



但是,如果是case ,是否还有一些方法仍然从内部函数的控制台调用变量 y ? (使用符合范围的符号,例如 outer.y



编辑:其他语言的调试器



显然,调试器的这种行为不限于javascript。 Python调试器 pdb 的行为类似:

  def outer() :
x = 5
y = 2
def inner():
print x
import pdb; pdb.set_trace()
inner()
outer()

(Pdb)x
5
(Pdb)y
** * NameError:'y'未定义


解决方案

是JavaScript引擎的优化。由于您不是在内部函数中引用 y ,所以在关闭中不需要保留它。这将允许它在外部函数返回时被垃圾回收。



如果添加对 y的引用例如 console.log(x,y)),您可以看到 x y 如你所料。


没有一些方法仍然调用变量y从内部功能的控制台?


显然不是。您可以在调试(不必做任何事情,任何参考)的情况下,在内部中添加引用 y 将会做)。


From the jQuery docs javascript guide:

Because local scope works through functions, any functions defined within another have access to variables defined in the outer function:

function outer() {
    var x = 5;
    var y = 2;
    function inner() {
        console.log( x );
        debugger; // <-- !!!
    }
    inner();
}
outer()

Console triggered with debugger:

> x
5
> y
ReferenceError: y is not defined

Since variables defined in the outer function can be used by the inner function (E.g. x or y), why is the debugger not able to call the y variable?

I suspect people will answer that the debugger only shows variables defined in the most inner/local scope. The reason for this being that otherwise no distinction could be made using the debugger between the inner and outer scope when inspecting a variable using the debugger in the inner function. Additionally, every variable defined in an outer scope which is executed in the inner scope allows the debugger to access it.

But if that is the case, isn't there some way to still call the variable y from the console inside the inner function? (using a notation respectful of scope, e.g. outer.y)

Edit: Debuggers in other languages

Apparently this behavior of a debugger is not limited to javascript. The Python debugger pdb for example behaves similarly:

def outer():
    x = 5
    y = 2
    def inner():
        print x
        import pdb; pdb.set_trace()
    inner()
outer()

(Pdb) x
5
(Pdb) y
*** NameError: 'y' is not defined

解决方案

Presumably this is an optimisation by the JavaScript engine. Since you're not referring to y within the inner function there is no need to hold on to it in the closure. This will allow it to be garbage collected when the outer function returns.

If you add a reference to y (for example console.log(x, y)) you can see the values of both x and y as you would expect.

isn't there some way to still call the variable y from the console inside the inner function?

Apparently not. You could just add a reference to y within inner while debugging (it doesn't have to do anything, any reference will do).

这篇关于调用内部函数调用外部函数中定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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