javascript - When should I used Douglas Crockfords implementation of "that"? -
in douglas crockford's article, private members in javascript, uses variable "that" refer "this" use in privileged method of class. have been using "this.publicmember" in code , seems work right. think time need use "that" if calling function "this" different, i.e. calling settimeout. when should 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 = this; this.service = function () { return dec() ? that.member : null; }; }
versus:
this.service = function () { return dec() ? this.member : null; };
he writes in article:
by convention, make private
that
variable. used make object available private methods.
then uses that
everywhere, avoid issues unbound functions (like settimeout
mentioned) , able switch "method" between private , privileged. since method instance-specific anyway (not inherited prototype or so), doesn't hurt make bound , access 1 more closure variable.
Comments
Post a Comment