Last active
August 29, 2015 13:56
-
-
Save chemdemo/9106001 to your computer and use it in GitHub Desktop.
`this` in javascript
This file contains 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
// `this` in javascript functions points to the object called it!! | |
function f() { | |
undefined !== this.x ? this.x ++ : (this.x = 1); | |
} | |
var o = { | |
f: f | |
}; | |
o.f(); | |
o.f(); | |
o.x; // => 2 | |
f(); | |
window.x; // => 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment