Skip to content

Instantly share code, notes, and snippets.

@rgrove
Created March 15, 2012 21:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgrove/2046918 to your computer and use it in GitHub Desktop.
Save rgrove/2046918 to your computer and use it in GitHub Desktop.
How `this` works in browser JS
console.log(this); // => window (the global object)
var myObject = {};
function foo() {
console.log(this);
}
foo(); // => window
foo.call(myObject); // => myObject
myObject.foo = foo;
myObject.foo(); // => myObject
var fooReference = myObject.foo;
fooReference(); // => window
@jhubert
Copy link

jhubert commented Mar 15, 2012

context == containing object

"the key is just that the context is bound at call time. It doesn't follow the function around."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment