Skip to content

Instantly share code, notes, and snippets.

@ReaperUnreal
Created March 28, 2012 14:21
Show Gist options
  • Save ReaperUnreal/2226560 to your computer and use it in GitHub Desktop.
Save ReaperUnreal/2226560 to your computer and use it in GitHub Desktop.
wtfjs
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