0

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.

klewis
  • 7,459
  • 15
  • 58
  • 102
  • As `Player` is uppercase, shouldn't it be a constructor? – Bergi Oct 02 '12 at 13:57
  • 1
    Where does `oplayer` come from? – bfavaretto Oct 02 '12 at 14:03
  • Oo - good call, which may invalidate my answer. I think we need to see some of that ...'d code! – ndtreviv Oct 02 '12 at 14:07
  • I am converting my constructor function into a revealing module patter, so I originally had oplayer = this; But if that is not needed my case, I will gladly change things up. Learning this stuff for the first time. – klewis Oct 02 '12 at 14:18

2 Answers2

1

Please first check out what the this keyword is, there is a good introduction at MDN.

Is it ok to "return this" if I am explicitly returning something?

Yes, of course. This is the default pattern for chainability of methods - everything returns the object it was called on. However, to rely on that you'd need to return it in every case, not only when the shields are down.

If this wasn't your aim, just use a normal return; statement (results in undefined as like as no return statement).

in context to using the revealing module pattern

It does absolutely not matter where you defined that function.

transform my function to be used in a proper context?

(I assume with "context" you refer to the this object here)

You can't really, the value of this always depends on the invokation of the function. Of course, you could .bind() the function to your player object or just directly only return player instead of this.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thank you Bergi. I think I simply want to return true for oplayer.damage. The reason is because I will checking it later like this outside of the module... if(mplayer.damage) { //then do something!} – klewis Oct 02 '12 at 14:09
1

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:

var test = function(){
   var pri = function(){
       console.log("Private");
   };

   var pub = function(){
       pri();
       console.log("pub");
       return this;
   }

   return {
       pub: pub
   };
}();

console.log(test.pri); //-> undefined
console.log(test.pub); //-> function(){…}
console.log(test.pub()); //-> "Private" "pub" [Object {…}]
console.log(test.pub().pri); //-> "Private" "pub" undefined
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
ndtreviv
  • 3,473
  • 1
  • 31
  • 45
  • What do you mean by `private pub`? It just returns `test` – Bergi Oct 02 '12 at 14:07
  • @Bergi if you run test.pub() it will output the text "Private" followed by "pub" into the console. – ndtreviv Oct 02 '12 at 14:09
  • @Bergi - copy and paste it into a FireBug console and click Run – ndtreviv Oct 02 '12 at 14:10
  • ah, OK; though in your example the line would output `"private" "pub" [Object]` and the last line would output `"private" "pub" undefined` - a bit confusing as in the first two lines you commented the result of the outer log – Bergi Oct 02 '12 at 14:11
  • Thanks ndtreviv - that helps me a lot – klewis Oct 02 '12 at 14:12
  • @Bergi, correct, because pub() returns the object, and I'm console.log-ging the result of the call. Want me to correct it? – ndtreviv Oct 02 '12 at 14:13
  • @Bergi, well I corrected it to show FireBug output and you changed it to show IE console output. Ho hum. – ndtreviv Oct 02 '12 at 14:20
  • @ndtreviv: I don't know what IE console is at all… I only wanted to correct the last line, and pretty the formatting a bit – Bergi Oct 02 '12 at 14:22
  • nedtreviv, did you have to use return this; or could you of also used a return; would either case give us the same comments you have in your demo? – klewis Oct 02 '12 at 15:06
  • 1
    @blachawk If I didn't return this then test.pub().pri would have errored. Try it for yourself - paste the whole code into a FireBug console window, remove the this from after the return statement and click run – ndtreviv Oct 02 '12 at 15:10