Skip to content

Instantly share code, notes, and snippets.

@W-Mills
Created August 20, 2019 19:56
Show Gist options
  • Save W-Mills/b2ff8572aa5bee9cb791f7317d90282f to your computer and use it in GitHub Desktop.
Save W-Mills/b2ff8572aa5bee9cb791f7317d90282f to your computer and use it in GitHub Desktop.
Clarifying this in javascript example 1
const foo = {
bar: 'baz',
getBar: function() { // assigned as a property on an object, getBar is a method
console.log(this.bar); // the value of this here is the direct-parent foo object context
},
};
foo.getBar() // logs 'baz'
const qux = foo.getBar; // assigning the function stored as the method getBar to the variable qux in the global scope
qux() // logs undefined, then returns undefined => why?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment