Skip to content

Instantly share code, notes, and snippets.

@palcisto
Created May 14, 2024 01:54
Show Gist options
  • Save palcisto/8a6689b18a48d74e8bdcddc497a936e6 to your computer and use it in GitHub Desktop.
Save palcisto/8a6689b18a48d74e8bdcddc497a936e6 to your computer and use it in GitHub Desktop.
JavaScript "this" keyword behavior example snippet
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