Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I find that in Javascript there are many methods of creating an object so what would be the appropriate approach for constructing an object and why.

parent = {
  init: function(option){
    if (option == 1)
      _.extends(this, sub1)
    else
      _.extends(this, sub2)
  },
  greet: function(){
    alert('Hello, my name is ' + this.get_name())
  }
}
sub1 = {
  get_name: function(){ return 'new month' }
}
sub2 = {
  get_name = function(){
    return 'current month'
  }
}
factory = function(option){
  return parent.init(option);
}

or

parent = function(){
  this.greet = function(){
    alert('Hello, my name is ' + this.get_name())
  };
}
children1 = function(){
  var self = new parent();
  self.get_name = function(){ return 'new month' };
  return self;
}
children2 = function(){
  var self = new parent();
  self.get_name = function(){ return 'current month' };
  return self;
}

factory = function(option){
  if (option == 1)
    return new children1();
  else
    return new children2();
};
share|improve this question

closed as unclear what you're asking by Joseph the Dreamer, Mat's Mug, syb0rg, palacsint, Vogel612 Apr 24 at 22:35

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
This isn't so much a code review as it is a question. This StackOverflow question might help: stackoverflow.com/questions/927651/… –  Greg Burghardt Apr 24 at 15:39
    
This is not just about how to create a class in Javascript. And its about how to write a factory class in javascript and what are the advantages of using one method over the other in terms of clean code and compliance with what javascript thinks is a good approach. If both of the approaches are wrong what would be a good one. –  user1634074 Apr 24 at 15:58
    
See my answer, in JS, the constructor is the factory. There is no need for another layer. –  konijn Apr 24 at 16:04
1  
So it appears the real question is about how to write factory classes or functions in JavaScript. –  Greg Burghardt Apr 24 at 16:31

2 Answers 2

up vote 3 down vote accepted

Factory classes in any language can be a benefit. There really is no one right way to do it.

Between your two examples, I like your second approach, where the factory itself contains the logic of knowing which class to instantiate and return. Really, this is the only guideline I recommend you follow. The factory object or function itself contains logic for instanting new objects.

Your implementation of inheritance, on the other hand, is not so ideal.

First, as a matter of practice "classes" in JavaScript start with a capital letter. Secondly, the proper way to set up prototypal inheritance is:

function Parent() {}

Parent.prototype.bar = 5;

function Child() {
    Parent.call(this);
}

Child.prototype = Object.create(Parent.prototype);

Child.prototype.foo = function() {
    alert(this.bar); // inherited from Parent
};
share|improve this answer
    
I understand. But what happens when you want to access properties of the Child class with the foo function. Ex: function Parent(){ this.year = 10} Children.prototype.foo = function() { return this.year * 10; } –  user1634074 Apr 24 at 17:09
1  
Amended my answer to show you how. –  Greg Burghardt Apr 24 at 17:12
    
How can I distinguish which properties go into Parent.prototype and which should be inside the class function scope ? –  user1634074 Apr 24 at 17:15
    
That all depends on who needs access to that data. If only the Child class needs access, define it as a variable in the function scope of the Child constructor function. If the public and/or child classes need access, put it on the Parent's prototype. –  Greg Burghardt Apr 24 at 17:16

Neither approach is fantastic.

If your constructor has functions that are the same for every instance, then you should add those to the prototype of the constructor.

I assume you are not a native English speaker, but children1 and children2 should be Child1 and Child2 at least. All constructors should start with a capital letter.

The most appropriate way to build OO objects depends on how you are going to use them [choose the right tool for the job], so answering that for this theoretical code is silly.

If the provided code was actual code, I would go with

function Parent( option ) {
  //Too length for a ternary
  if( option === Parent.MEANINGFUL_CONSTANT)
    this.name = 'new month'
  else
    this.name = 'current month';
}

Parent.prototype.greet = function(){
  //Nobody uses alert, it is annoying
  console.log('Hello, my name is ' + this.name )
}

Parent.MEANINGFUL_CONSTANT =1;

parent = new Parent( Parent.MEANINGFUL_CONSTANT );
parent.greet();

I dropped the factory thing, to encourage a direct call to Parent; factories are more of a Java thing in my mind. I also changed the named getter to a public property which is more the JS way to go.

share|improve this answer
    
Sorry but this is not very helpful. Its just solving this particular problem. I'm looking for a function/class that you can call and it will return an object that some different functions and some similar functions depending on the context. So there will be an inheritance or the object itself will append different functions depending on what option is. –  user1634074 Apr 24 at 16:10
2  
The quality of the answer is a direct result of the quality of the question :\ –  konijn Apr 24 at 16:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.