Last active
November 15, 2017 23:59
-
-
Save eagleusb/958f45c1bd8d5ccae6f7e81855f770a1 to your computer and use it in GitHub Desktop.
A `this` context reminder for NodeJS
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
#!/usr/bin/node | |
// non-strict mode = `this` context is 'global' | |
// strict-mode = `this` context is undefined | |
function parentFunction() { | |
this.foo = 'bar'; | |
console.log('parentFunction scope : ' + this.foo); | |
console.log('parentFunction scope : ' + this); | |
function firstLevelFunction() { | |
console.log('firstLevelFunction scope : ' + this.foo); | |
console.log('firstLevelFunction scope : ' + this); | |
} | |
const firstLevelArrowFunction = () => { | |
console.log('firstLevelArrowFunction scope : ' + this.foo); | |
console.log('firstLevelArrowFunction scope : ' + this); | |
}; | |
firstLevelFunction(); | |
firstLevelArrowFunction(); | |
} | |
parentFunction(); | |
// set the context with call() method | |
parentFunction.call('superContext'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment