What is a constructor in programming?

699 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 class definition defines what a class looks like. What attributes objects of that class have, what methods they have, etc. Think of it like saying “a chair must have legs/some kind of support, a seat, and a back.” 

 A constructor creates an instance of a class, giving values to those attributes. Think of it like building an actual chair out of specific legs, seat and back.

Remember that a class is a definition of what it means to be a chair, and an instance/object of that class is an actual chair. Constructors build specific chairs. 

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