The console area of โ€‹โ€‹the Firebug console. Why isn't "this" always the same thing?

Firebug Console. Why isn't "this" always the same thing? Shoudn't it be a โ€œwindowโ€ all the time?

+6
javascript debugging firebug
source share
3 answers

The this value in the console will be the same as the this value in the current executable code. 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 breakpoint on the line x = {n:12} , switch to the console, you will see that this is a window. However, when you go to the alert this line, the console contains an object held by the variable x . IOW there is no difference between this in the executable context and the console. For this reason, you can use the console to adjust the values โ€‹โ€‹of variables and properties during debugging.

+3
source share

In a function called directly without an explicit owner object, the value of this will be the default object (window in the browser).

In a function called using the method, call syntax, for example objname.myFunction() or objname['myFunction']() , calls the value of this must be objname.

Learn more about calling JavaScript functions in JavaScript.

JavaScript, 5 ways to call a function

+2
source share

This keyword always refers to the owner of the called function. You can read a clear and detailed explanation of this here .

From the article I referred to above, I think this is most clearly explained:

alt text

+2
source share

All Articles