Skip to content

Instantly share code, notes, and snippets.

@jaseemabid
Forked from kaaes/simple_this_ex.js
Created August 23, 2011 14:06
Show Gist options
  • Save jaseemabid/1165210 to your computer and use it in GitHub Desktop.
Save jaseemabid/1165210 to your computer and use it in GitHub Desktop.
basic example of this object
var a, b, c, d, e;
a = function(obj) {
return this;
}
// when you call the function in 'function form' 'this' is window object
a(); // window
// Always! Even if you define and call it inside another function
(function() {
var aa = a;
var bb = aa();
return bb; // window
})();
// when you call it as a method of some object 'this' will be that object
b = { a : a };
b.a(); // b object
// when you use the function as a constructor with 'new' operator new
// object will be created inside, assigned to 'this' and then returned
c = new a(); // object a - instance of the a constructor function
// when you use the function as a callback for some events on DOM element
// it will be the context of the function
d = document.getElementById('myDiv');
d.addEventListener('click', a, false); // HTMLDivElement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment