That this depends on how you call the function in which you use this .
1. call as a function
functionName();
In this case, this will always refer to the global object (most often the window object).
Example
a = 2; function XY(a) { this.a = a; this.b = function () { func(); }; function func () { console.log(this.a); } } var xy = new XY(1); xy.b();
Note
- The example is a little constructed, but note that the
func function is called simply by writing func(); . Therefore, even if your function is inside the constructor function ( XY ) and called from a function that is called as a method (see Clause 3), this still applies to the global object.
2. call with a new keyword
var obj = new functionName();
In this case, this will refer to the newly created object.
Example
a = 2; function XY(a) { this.a = a; } var xy = new XY(1); console.log(xy.a);
3. call as a method
obj.functionName();
In this case, this will refer to the object containing the function that you are calling.
Example
a = 2; var xy = { a: 1, func: function() { console.log(this.a); } } xy.func();
4. call with apply
functionName.apply(thisObj, argArray);
In this case, this will be a new Object(thisObj) , with thisObj being the first argument to the apply function.
Example
function xy (a,b) { console.log(this); } xy.apply({f:3}, [1,2]); //Object {f: 3} xy.apply("hello", [1,2]); //String {0: "h", 1: "e", 2: "l", 3: "l", 4: "o"}
5. event handler call
As a Mifeet user suggested, events also change the context of this :
Events are a separate issue. They are not part of EcmaScript and are typically handled differently by different browsers. However, the differences regarding this not significant for IE> 8, as far as I know, nonexistent.
this will refer to the DOM element that triggered the event, unless you use the inline event handler. In this case, this will refer to the global object.
Example
<button id="1" onclick="clickit()">click me</button> <button id="2">click me</button> <button id="3">click me</button> <script> var button1 = document.getElementById("1"); var button2 = document.getElementById("2"); var button3 = document.getElementById("3"); id = "0"; window.clickit = function(){ console.log(this.id); }; button2.onclick = clickit; </script>
Event Notes
- Internet Explorer prior to version 9 did not support
addEventListener , but attachEvent . Using this function will also result in this related to the global object. this , directly inside the HTML tag, will refer to the DOM element representing this tag. Therefore, <button id="1" onclick="clickit(this)">click me</button> will pass the DOM element to the clickit event clickit .
back to your case
In the first two cases, you call your function as a method, and in the latter case, you call it as a function. This is why in the latter case, this refers to a global object.
EDIT
I recently saw a very similar answer to a very similar question here on StackOverflow. Unfortunately, I could no longer find it, so I decided to send the answer myself. But if someone knows what I mean, just leave a comment, and I will gladly put a link to the original answer in my answer.