Last active
February 21, 2019 11:36
-
-
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
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
| // 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