Why use classes when you have factory functions?


#1

I don’t see any big differences between their powers and uses.


#2

Factory functions are function factories.
Classes are object factories.
Objects !== Functions


#3

Oh, right. Guess I have to slow down and try to remember all this material.

Wait, but what is that supposed to mean? A function can make functions? I just went back to the lesson on Factory Functions, and it says it makes objects. :confused:


#4

See, https://www.codecademy.com/courses/introduction-to-javascript/lessons/advanced-objects/exercises/factory-functions?action=resume_content_item


#5

From what I’ve seen so far, a Factory Function is not a function factory - pretty sure the only way to make functions is to use function keyword or variables defined as a function - but rather a function that is a factory, a factory function. A factory function returns an object, which can use the function’s parameters as its key-value pairs. Then you can call the function with the values as its parameters. This creates an object with those key-value pairs.

Thanks for the answer @dugong-s, you made me go back to the lesson and review the material, and now I think I understand these factory functions quite a bit better. :slight_smile:

Maybe they can be used to create functions - I don’t know about that for sure - but they can create objects, and now I don’t see a point to classes. I think there must be some sort of underlying difference, but at the moment, I can’t find one.


#6

I am sorry. It is my fault. Yes you can create functions that is creating objects. Classes are to determine a template for the objects you are going to create using the class. So that means, you are going to spent much less time to create objects which are sharing properties and methods.
So, I think you will understand the difference between classes and factory functions since you reviewed the lesson.


#7

I have looked up some answers and it appears that both classes and factory functions are used for pretty much the same thing. Here’s a short list of what classes & constructor functions do that factory function’s can’t:

  • Most books teach you to use class or constructors.
  • this refers to the new object.
  • Some people like the way myFoo = new Foo() reads.
  • There may be a micro-optimization performance benefit, but you should not worry about that unless you have profiled your code and proven that it’s an issue for you.

From https://medium.com/javascript-scene/javascript-factory-functions-vs-constructor-functions-vs-classes-2f22ceddf33e

I think the only real difference would be the ‘this’ part. Apparently, constructor functions get the properties of the new object, but factory functions get the properties of the old one. Well, in a constructor function, you would use this.property = property, and in a factory function, you would use property: property, or simply property, to assign the values of the new object. I’m not sure what the ‘this’ thing is about.


#8

What about creating parent classes? That create classes.
It is all about being more concise (and faster, as a result) and readable to the human. I suggest you read and apply everything that is explained in classes lessons again.


#9

OK, I will.

I haven’t finished the entire classes chapter yet so I wouldn’t know much of the differences. And parent classes that create classes? I guess those could be a pro for classes. I don’t get the concise part though. If it’s faster, more concise, and more readable, why use factory functions when you have classes? Are they just “there”?


#10

As far as I’m concerned, classes is one of the newest features of ES6 to create objects with a easier syntax and it offers inheritance possibilities from parent to child classes. So, I am not a pro, I have just passed through classes lessons. I can’t tell you why, but it seems easier to me to create objects through classes.