In Douglas Crockford's article, Private Members in Javascript, he uses the variable "that" to refer to "this" for use in the privileged method of the class. I have been using "this.publicMember" in my code and it seems to work right. I would think the only time you would really need to use "that" would be if you are calling the function where the "this" would obviously be different, i.e. calling it from setTimeout. When should I use / not use "that" ?
function Container(param) {
function dec() {
if (secret > 0) {
secret -= 1;
return true;
} else {
return false;
}
}
this.member = param;
var secret = 3;
var that = this;
this.service = function () {
return dec() ? that.member : null;
};
}
Versus:
this.service = function () {
return dec() ? this.member : null;
};