Firebug控制台窗口范围。为什么不是“这个”总是一样? [英] Firebug console window scope. Why isn't "this" always the same?

查看:107
本文介绍了Firebug控制台窗口范围。为什么不是“这个”总是一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebug控制台范围。为什么不这总是一样的?不要一直在窗口?

解决方案

的价值将与当前正在执行的代码中 的值相同。考虑: -

  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屋!

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