What is a constructor in programming?

721 viewsOtherTechnology

I am still trying to wrap my head around the concept I learned in class a while ago. We learned briefly about constructors in JavaScript. Can someone please ELI5? Thank you!

In: Technology

18 Answers

Anonymous 0 Comments

A constructor is just a special function.

Lets start from classes. Classes are blueprints for creating objects. You do this by calling new “Class name”

Now lets say you want to make your objects a bit different based on what you give it as parameters when you create it. This is where constructors come in. They can only be called once, and only during object creation.

That is why they are special. Then they use those parameters to fill some fields that are part of your class (or do something else) and that’s that.

You are viewing 1 out of 18 answers, click here to view all answers.