Skip to content

Instantly share code, notes, and snippets.

@dominikkaegi
Last active February 21, 2019 11:36
Show Gist options
  • Select an option

  • Save dominikkaegi/6b0a9afd3874c25c20e3c5495e1b53e9 to your computer and use it in GitHub Desktop.

Select an option

Save dominikkaegi/6b0a9afd3874c25c20e3c5495e1b53e9 to your computer and use it in GitHub Desktop.
This gist showcases the difference between the static binding of the `this context` with ES6 arrow functions and dynamic binding with declarative functions
// ES 6 Arrow Function vs Function
// showcases the difference between the static binding of the `this context`
// with ES 6 Arrow Function and dynamic binding with normal Functions
var obj = {
dynamicBinding() {
console.log('Dynamic Context: ', this)
window.setTimeout(function() {
console.log('T Dynamic Context: ', this),
0
})
},
staticBinding() {
console.log('Dynamic Context: ', this)
window.setTimeout(() => {
console.log('T Static Context: ', this),
0
})
}
}
obj.dynamicBinding()
// Dynamic Context: : {dynamicBinding: ƒ, staticBinding: ƒ}
// T Dynamic Context Binding: Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, parent: Window, …}
obj.staticBinding()
// Dynamic Context: {dynamicBinding: ƒ, staticBinding: ƒ}
// T Static Context Binding: {dynamicBinding: ƒ, staticBinding: ƒ}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment