Created
November 2, 2017 17:23
-
-
Save spencercwood/72a5d5ca5b067a17b5d07fb6ec59bf12 to your computer and use it in GitHub Desktop.
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
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