Asked  6 Months ago    Answers:  5   Viewed   23 times

Can someone clarify the difference between a constructor function and a factory function in Javascript.

When to use one instead of the other?

 Answers

69

The basic difference is that a constructor function is used with the new keyword (which causes JavaScript to automatically create a new object, set this within the function to that object, and return the object):

var objFromConstructor = new ConstructorFunction();

A factory function is called like a "regular" function:

var objFromFactory = factoryFunction();

But for it to be considered a "factory" it would need to return a new instance of some object: you wouldn't call it a "factory" function if it just returned a boolean or something. This does not happen automatically like with new, but it does allow more flexibility for some cases.

In a really simple example the functions referenced above might look something like this:

function ConstructorFunction() {
   this.someProp1 = "1";
   this.someProp2 = "2";
}
ConstructorFunction.prototype.someMethod = function() { /* whatever */ };

function factoryFunction() {
   var obj = {
      someProp1 : "1",
      someProp2 : "2",
      someMethod: function() { /* whatever */ }
   };
   // other code to manipulate obj in some way here
   return obj;
}

Of course you can make factory functions much more complicated than that simple example.

One advantage to factory functions is when the object to be returned could be of several different types depending on some parameter.

Tuesday, June 1, 2021
 
TheCarver
answered 6 Months ago
59

This should answer it: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself and http://www.codinghorror.com/blog/2007/03/curlys-law-do-one-thing.html

Curly's Law, Do One Thing, is reflected in several core principles of modern software development:

  • Don't Repeat Yourself

    If you have more than one way to express the same thing, at some point the two or three different representations will most likely fall out of step with each other. Even if they don't, you're guaranteeing yourself the headache of maintaining them in parallel whenever a change occurs. And change will occur. Don't repeat yourself is important if you want flexible and maintainable software.

  • Once and Only Once

    Each and every declaration of behavior should occur once, and only once. This is one of the main goals, if not the main goal, when refactoring code. The design goal is to eliminate duplicated declarations of behavior, typically by merging them or replacing multiple similar implementations with a unifying abstraction.

  • Single Point of Truth

    Repetition leads to inconsistency and code that is subtly broken, because you changed only some repetitions when you needed to change all of them. Often, it also means that you haven't properly thought through the organization of your code. Any time you see duplicate code, that's a danger sign. Complexity is a cost; don't pay it twice.

Saturday, May 29, 2021
 
Yarin
answered 7 Months ago
100

If you're likely to want to combine the result of this piece of code with other tables, then obviously a table-valued function will allow you to compose the results in a single SELECT statement.

Generally, there's a hierarchy (View < TV Function < Stored Proc). You can do more in each one, but the ability to compose the outputs, and for the optimizer to get really involved decreases as the functionality increases.

So use whichever one minimally allows you to express your desired result.

Sunday, June 27, 2021
 
lena
answered 6 Months ago
25

A primary reason to define a method on the object's prototype rather than in the constructor is that the method defined in the prototype is immediately available to and shared by (in memory) all instances of the object.

By defining the method in the constructor, that method is specific to the instance of the object creating it. If you insantiate 10 of your object, you'll have 10 copies of the method in memory, even though they are all the same, until you modify one of them.

To be clear though, by shared among object instances, I do not mean to say that the method operates statically on any properties it accesses. Those properties (if defined with this.) are still instance-specific. It's just that you don't end up defining multiple copies of the same method.

Wednesday, September 22, 2021
 
Pupil
answered 2 Months ago
45

Checkout TypeScript. It's awesome language - a javascript superset and it compiles to pure Javascript which you can run everywhere.

Any JS code is still valid TS code. What TS gives you is a OO approach, type checking, static analysis and new features. When you write code you can immediately start using features from ES6/7 or even ES8 and compile it to ES5 - most supported JS version.

More info: https://www.typescriptlang.org/

Monday, November 22, 2021
 
Shoarc
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share