Skip to content

Instantly share code, notes, and snippets.

@spencercwood
Created November 2, 2017 17:23
Show Gist options
  • Save spencercwood/72a5d5ca5b067a17b5d07fb6ec59bf12 to your computer and use it in GitHub Desktop.
Save spencercwood/72a5d5ca5b067a17b5d07fb6ec59bf12 to your computer and use it in GitHub Desktop.
var someObj = {
someMethod: function() {
this.someProp = 'huzzah!';
console.log(this);
// this refers to someObj:
// { ..., someProp: 'huzzah!' }
someNestedFunction();
function someNestedFunction() {
console.log(this);
// this now refers to someNestedFunction context,
// which is different than someObj
// Solution? var self = this (outside the nested function)
// self will always refer to someObj context
}
}
}
someObj.someMethod();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment