Constructor function

hello, I’ve a big problem here. I recently started learning OOP and the first thing I started with was the constructor function, I find it very very useful for inheritance and I made a books library project with it. but in the course (the odin project) that I learn from; after learning constructor they said It’s not good and mention us a link which explain why we shouldn’t use constructor function. and this is the link if anyone needs to read it >> Constructors Are Bad For JavaScript
this article was written in 2013

my problem is I can’t understand his point about why we shouldn’t use constructor function. or to be specific I can’t understand what the problem with constructor functions. so If anyone can give me another source explains the problems related to the constructors in js, and what the instanceOf and what the difference between it and prototype; I’ll be so grateful
I searched alot to understand, but my mind is so scattered now.

The ‘problem’ is that some people like to make hyperbolic ‘purist’ statements. Use whatever works for your needs.

1 Like

It really seemed to me at first that he was talking about his personal opinion. I know that everything has pros and cons, but the title of his article confused me a lot, which made me search a lot if the constructor function is not really used because of security problems or something like that, such as using eval() for example.
So thank you for pointing out that it is a bit of an exaggeration .
or perhaps it’s an inappropriate article for someone who is still at the beginning of his OOP journey, I think :face_with_diagonal_mouth:

You really should be using a class instead, which explicitly requires the new invocation making the argument a moot point I would think.

I didn’t really read the article but I’m guessing it is pretty outdated. I don’t think using factory functions is considered better (or worse I guess) by most people today.

I almost never do OOP so I’m not the right person to comment on this but I wouldn’t suggest trying to replicate classical OOP in JavaScript as it isn’t really built for that. I think most people use it more as a code-organizing tool and not so much for inheritance/extensibility.

1 Like

I understand you, I havn’t learnt Classes yet, but there’s a constructor in it,I think, which makes me comfortable with Classes. constructor seems more organised and clear than factoury function,which he recommended, at least :grin:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.