Skip to content

Instantly share code, notes, and snippets.

@mde
Created May 19, 2015 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mde/3825a3732effbe76ee62 to your computer and use it in GitHub Desktop.
Save mde/3825a3732effbe76ee62 to your computer and use it in GitHub Desktop.
JavaScript "this"
var foo = {
a: function () {
console.log(this);
}
};
foo.a(); // Logs the foo obj
foo['a'](); // Same, logs the foo obj
var bar = foo.a;
bar(); // Logs the global obj
var Baz = function () {
// Implicit 'this' created
console.log(this);
this.a = true;
this.b = 'whatever';
// Implicit 'this' returned
};
// Using `new` puts it into constructor mode
var qux = new Baz(); // Logs the implict new instance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment