What is a constructor in programming?

701 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.

Anonymous 0 Comments

A constructor is a method that initializes an object instance. <- that is all. It’s usually tied directly to the object and is usually called automatically by the language when you instantiate a new copy of the object.

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.

Anonymous 0 Comments

Constructors are programs that will automatically be executed when a new object is created.
Every object can have zero or one constructors attached.
The constructor can only run once per object.
If more objects of the same type are created still each object runs it’s own constructor once.

Therefore the main purpose of the constructor is to initialize certain values within the object before the object is actually used.

Anonymous 0 Comments

When you create an instance of a class- “you create a variable of your own type” This is an object, which can have more than one attribute to it, more than one value assigned under a single name.

So if you are creating a new “variable of this type” and sending it some values, how will the compiler know which value goes to which attribute of the “multi variable”?

Simple, you just tell it. A constructor is a function that runs when you’re creating an object of the type of your class.
In this you can tell it which attribute gets which value.

But since it is just a function that’s being called when an object is created, why not have it do more than just assign stuff. You can also call some initialising functions of your own.

A good example of this is if you have a class rectangle. Now if you create a shape of this type (object instantiation), you will have to tell which value is the Length and which is the Width.

So you can create a constructor and use it to assign each of its attribute with these values. But that’s not all, you can also use the constructor to call a function that can take these values and calculate its area, and save it in another attribute Area.

All this when the object is created itself instead of manually going and assigning each object attribute.

Anonymous 0 Comments

Basically, a constructor is just a function inside the class that can take arguments and is used to create an object in return. 

What it will do in order is that it will first allocate/reserve the right amount of memory for the object (this step is often hidden away in languages like JavaScript, Java or Python). Then it will initialise the object with either default values or the arguments you provided to it. Finally, it will return you the initialised object so you can use it afterwards in other parts of your code.

Similarily, in some languages like C where you have to manually manage your memory, there’s also the concept of a destructor which is simply the opposite of a constructor, so a function that takes the object as argument and will free the memory used by that object so that it is available again to other parts of the program.

Anonymous 0 Comments

Constructors are just instructions for set up:

Imagine you had a box labeled “cars”  you can reach into that box and magically pull car after car out.  But what state is it in?  Lights on or off? Doors open? Locked?

A constructor tells the magic making the cars how to initialize it: doors closed, lights off, gas tank empty etc.

Anonymous 0 Comments

Let’s say you own a bakery, and one of the things you do there is make cakes to order.

Bad ways to do that:

Make each customer tell you *exactly* how to make the cake they want.

Have completely separate instructions for how to make every possible variant of every cake. I’m talking having one recipe for how to make a 3 tier chocolate cake with “Happy Birthday” on it and another recipe for a 3 tier chocolate cake with “Congratulations” on it.

Accept orders without checking them. If it turns out that the customer ordered something impossible or didn’t give you enough info they only find that out a while later and the explanation is super confusing.

Every customer gets the exact same cake (this one would be totally fine for other situations, just not this one.)

Good way to do that:

1. Customers get an order form to fill out.

2. The order form gets checked, and if there’s something wrong the customer is told immediately in a way that’s easy to understand. “You have to put the number of tiers you want,” “we only have blue, pink, or green icing, not yellow,” “you can only order a vanilla gluten free cake, not a chocolate one.”

3. If the order form looks good then it goes to the back (where the customer can’t see) and is made according to a procedure that branches based on stuff like how many tiers the cake is.

A constructor is sort of like that order form, the procedure for checking the order form, and the procedure for making a cake based on the order form.