Created
May 14, 2024 01:54
-
-
Save palcisto/8a6689b18a48d74e8bdcddc497a936e6 to your computer and use it in GitHub Desktop.
JavaScript "this" keyword behavior example snippet
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
const foo = { | |
getArrow: () => this, | |
getWeird: function() { return this }, | |
getFunky() { return this }, | |
} | |
function factory() { | |
return { | |
getArrow: () => this, | |
getWeird: function() { return this }, | |
getFunky() { return this }, | |
} | |
} | |
const obj = factory() | |
console.log({ | |
arrow: foo.getArrow(), | |
weird: foo.getWeird(), | |
funky: foo.getFunky(), | |
}) | |
console.log({ | |
arrow: obj.getArrow(), | |
weird: obj.getWeird(), | |
funky: obj.getFunky(), | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment