views:

83

answers:

4

I am not exactly sure the differences between saying "function x(){}" or "this.x=function(){}", but I had to make an object oriented JavaScript which is laid out like this:

function PROJECT(){
  ...
  ...
  ...
  this.main_call=function(...){ return this.recursive_call(...); }

  this.recursive_call=function(...){
    ...
    var local,variables;
    function helper(...,ref){
      ...
      ref.recursive_call(...);
    }
    ...
    helper(...,this);
    ...
  }
}
x=new PROJECT();
x.main_call(input);

My question here is whether this is good style or if there is some way I can call the helper function (which is used to stop duplicating the same code over and over), without passing a "this" pointer. I am a bit concerned because helper function already accesses all the local variables in recursive_call, but it throws errors if I try to access this.recursive_call directly. I also ran into problems with variables overriding when trying to declare this.helper=function(...){...}.

A: 

I don't quite understand what you are trying to do but you could change your closures. Something like this

function PROJECT(){

    this.main_call=function(){
    return recursive_call();
    }

    var local,variables;
    function helper()
    {
    recursive_call();
    }

    function recursive_call(){
    helper();
    }
}
x=new PROJECT();
x.main_call(input);

If that doesn't suit what you need to do you could always change your recursive function to be something like this

this.recursive_call=function(...){
    ...
    var local,variables;
    var ref = this;
    function helper(...){
      ...
      ref.recursive_call(...);
    }
    ...
    helper(...);
    ...
  }
Revolution42
A: 

Check out Learning Advanced JavaScript from John Resig.

  1. If main_call only wraps recursive_call, it isn't needed.
  2. For helper functions, consider using function closures when creating the functions so that they aren't created for every call.
  3. I personally prefer using this in the helper instead of passing ref.

    this.recursive_call=(function(){
      ...
      function helper(...){
        ...
        this.recursive_call(...);
      }
      return function(...)
         ... 
         helper.call(this,...);
         ...
      };
    }).call(this);
    

While its a little more complicated, it keeps this useful throughout which feels more OO to me. Though you must always ask yourself what objects you need access to, where. If it isn't meaningful having this throughout, it isn't useful, don't use it. Revolution42's answer will suffice.

You might also consider some functional aspects. For example, pass the helper function to recursive_call.

nicerobot
A: 

To learn about function name () {} vs var name = function () {} I would suggest reading this related question:

/questions/336859/javascript-var-functionname-function-vs-function-functionname

Juraj Blahunka
A: 

Can you change it into a while/for loop? Recursion is often difficult to read and maintain (which is probably why you feel uneasy about your current code). You can still have a helper() function called by this.helper() but the decision to repeat the process is in the loop logic.

Watts