views:

266

answers:

4

I prefer to use functional OOP style for my code (similar to the module pattern) because it helps me to avoid the "new" keyword and all problems with the scope of "this" keyword in callbacks.

But I've run into a few minor issues with it. I would like to use the following code to create a class.

namespace.myClass = function(){
  var self = {},
      somePrivateVar1;

  // initialization code that would call
  // private or public methods
  privateMethod();
  self.publicMethod(); // sorry, error here

  function privateMethod(){}

  self.publicMethod = function(){};

  return self;
}

The problem is that I can't call public methods from my initialization code, as these functions are not defined yet. The obvious solution would be to create an init method, and call it before "return self" line. But maybe you know a more elegant solution?

Also, how do you usually handle inheritance with this pattern? I use the following code, butI would like to hear your ideas and suggestions.

namespace.myClass2 = function(){
  var self = namespace.parentClass(),
      somePrivateVar1;            

  var superMethod = self.someMethod;
  self.someMethod = function(){
    // example shows how to overwrite parent methods
    superMethod();
  };

  return self;
}

Edit. For those who asked what are the reasons for choosing this style of OOP, you can look into following questions:

+2  A: 

Several questions occur to me, such as why you want to avoid the new operator, and why you want to call functions before they are defined. However, leaving those aside, how about something more like the following?

namespace.myClass = function(){
  var somePrivateVar1;
  var self = {};

  // initialization code that would call
  // private or public methods
  privateMethod();
  publicMethod();

  function privateMethod(){}
  function publicMethod(){}

  self.publicMethod = publicMethod;
  return self;
}
Tim Down
Thanks, one of the problem with this approach is that you don't have a reference to a current object inside your class. But in my example, it was possible to pass an object itself to another function using self variable, ex. someFunctionThatRegistersAnObject(self);
valums
I tried to vote your answer, but stackoverflow shows an error "Vote too old to be changed, unless this answer is edited". Is it a bug?
valums
@valums: Answer changed to address your comment. Not sure about SO error, but you should be able to change your vote now I've edited the answer.
Tim Down
Thanks, this will work too, but I think that redeclaring methods at the bottom doesn't look very clean.
valums
It's not redeclaring a method as such: there's just two separate statements for declaring a function and then assigning that function as a method of `self`. I agree that the style isn't very pleasant but it's in response to your set of requirements.
Tim Down
+1  A: 

I agree with almost every comment or answer provided so far, but to take Tim's answer one step further - he questioned why you'd want to call a method before it was defined, but offered a solution anyway, whereas I'd suggest you shouldn't call before defining (I don't know of any language where calling a method prior to defining it or at least declaring it is considered good practice), so how about:

namespace.myClass = function(){  
  //declare private vars (and self) first
  var self = {},  
      somePrivateVar1;  

  //then declare private methods
  function privateMethod(){}  

  //then public/privileged methods
  self.publicMethod = function(){};  

  // THEN (and only then) add your 
  // initialization code that would call  
  // private or public methods  
  privateMethod();  
  self.publicMethod(); // no error any more

  //then return the new object
  return self;  
} 

Is there a particular reason why this would not work for you?

Graza
Thank you, this will work ok, but I would prefer to leave the constructor at top, as it's very common, and I don't want to break habits of other programmers.
valums
A: 

This is the solution I mentioned in the question. Your comments are welcome.

namespace.myClass = function(){
  var self = {},
      somePrivateVar1;

  function init(){
    privateMethod();
    self.publicMethod();
  }

  function privateMethod(){}
  self.publicMethod = function(){};

  init();
  return self;
}
valums
That will work, and if you are committed to having initialisation code at the top, before declaring any of your methods, would be the only way to go, as far as I could tell. My only comment is that this is not "elegant", which is what you have asked for, however my *personal* opinion is that your criteria for how you want the method to "look" is not elegant anyway - so within this criteria, what you have there is probably the best solution.
Graza
+2  A: 

Hi, there,

You can use Zet.js library which will help you with declaring classes in the way you want.

nemisj