Firebug控制台窗口范围。为什么不是“这个”总是一样? [英] Firebug console window scope. Why isn't "this" always the same?
问题描述
的价值$控制台中的c $ c>将与当前正在执行的代码中
的值相同。考虑: -
function outer()
{
//这是窗口
var x = {n:12};
var fn = function()
{
//这是对象{n:12}
alert(this.n);
}
fn.call(x);
}
...
< img src =thing.gifonclick =outer()/>
如果您在 x = {n:12}
行,切换到控制台你会发现这个
是窗口。但是,当您进入警报
行时,控制台中的
是 x
变量。 IOW在执行上下文和控制台之间的这个
之间没有区别。因此,您可以在调试时使用控制台来调整变量和属性的值。
Firebug console scope. Why isn't "this" always the same? Shoudn't it be "window" all the time?
The value of this
in the console will be the same as the value of this
in the code currently being executed. Consider:-
function outer()
{
// this is window
var x = {n:12};
var fn = function()
{
// this is object {n:12}
alert(this.n);
}
fn.call(x);
}
...
<img src="thing.gif" onclick="outer()" />
If you put a break point on the x = {n:12}
line, switch to console you will find the this
is the window. However when you step to the alert
line this
in the console is the object held by the x
variable. IOW there is no distinction between this
in the executing context and the console. Its for this reason that you can use the console to tweak values of variables and properties while debugging.
这篇关于Firebug控制台窗口范围。为什么不是“这个”总是一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!