Created
March 28, 2012 14:21
-
-
Save ReaperUnreal/2226560 to your computer and use it in GitHub Desktop.
wtfjs
This file contains 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 obj = { | |
val: 0, | |
double: function() { | |
function helper() { | |
this.val += this.val; //this refers to global scope | |
} | |
helper(); | |
} | |
}; | |
obj.val = 2; | |
obj.double(); //doesn't work | |
//obj.val is still 2 | |
//have to do it like this: | |
var obj = { | |
val: 0, | |
double: function() { | |
var that = this; | |
function helper() { | |
that.val += that.val; //that captured by closure | |
} | |
helper(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment