Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.