Created
May 19, 2015 23:49
-
-
Save mde/3825a3732effbe76ee62 to your computer and use it in GitHub Desktop.
JavaScript "this"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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