What is a constructor in programming?

731 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

I’m an old-school programmer. I don’t like OOP. It complicates shite unnecessarily with terminology and too much syntax.

A constructor is just a function/procedure that creates/builds/initialises a particular object.

If you have an object that represents, say, a display screen, the constructor would likely do things like:

– Allocate a screen buffer

– Initialise variables (properties/members of the object, etc.) like screen height, width, colour depth, etc.

– Initialise the graphics subsystems needed, etc.

It’s just the piece you call to “create” an object of that type, which in many languages happens automatically if you say “var this_display = new DisplayScreen” or whatever. When you do that, the constructor is automatically run, to initialise that new object you just created.

A constructor is nothing more than a function that builds an object, and an object is just a fancy concept given to a self-contained structure that has all its required variables and functions put together in one convenient place.

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