I like the revealing module pattern. I will have private functions that I would like to make public and return them. But I may also have some local functions within my revealing module pattern that “return this”…
var player = function(){
//my local variable scope...
oplayer.damage = function(){
if(!this.grace){
this.shield--;
if (this.shield == 0){
return this;
}
}
};
...
return {
damage : oplayer.damage
}
}();
Is it ok to “return this” if I am explicitly returning something? (in context to using the revealing module pattern). If not, how can I transform my local function oplayer.damage to be used in a proper context? Thanks for any advice! I’m just trying to wrap my mind around the whole “return this” concept.
It should be fine, because “this” is contextual to execution. Because you’re returning this on a public function which is part of the “cut down” object, then you’ll only get the cut down object. So, I think what you’re trying to do should be fine if you consider the following scenarios: