Skip to content

Instantly share code, notes, and snippets.

@eagleusb
Last active November 15, 2017 23:59
Show Gist options
  • Save eagleusb/958f45c1bd8d5ccae6f7e81855f770a1 to your computer and use it in GitHub Desktop.
Save eagleusb/958f45c1bd8d5ccae6f7e81855f770a1 to your computer and use it in GitHub Desktop.
A `this` context reminder for NodeJS
#!/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