Imagine you are creating a list of participants and have to set up the first name, last name, and the name output by hand for every new entry. That costs time and mistakes quickly creep in. This is exactly where a constructor comes in. It prepares an object at the moment it is created, so you can pass in the data directly. In this tutorial, you will build a small Person function step by step, get to know the keyword this, and see how to pass values directly when creating objects.
Key Takeaways
- A constructor is a function that is called when an object is created with
newand defines the basic structure of the object. - With
this, you refer inside the constructor to the instance that is currently being created. - Methods defined in the constructor are available to every new object without you having to write them again each time.
- Through parameters, you can pass values directly when creating an object. This saves intermediate steps and makes your code clearer.
- The notation
new Object()produces the same result as an object literal with curly braces.
Step-by-Step Guide
Step 1: Recognizing the Problem with Repeated Code
Before you write a constructor, it is worth looking at the problem. If you create individual objects without a template, you have to set the first name and last name separately for each object. It gets even more unpleasant with methods. You would have to define a function that assembles the full name again for every new object. An external function with a reference would be one way out, but with many objects the solution remains cumbersome. A constructor offers an elegant answer to this.

Step 2: Creating the Constructor Function
Now create a function named Person. Inside this function, you access the properties of the object with the keyword this. First, you set this.vorname and this.nachname to empty values. This establishes the basic structure, and every person you create later will have these two properties.
Step 3: Defining a Method in the Constructor
Now the advantage of the constructor comes into play. You assign to the property this.getName a function that combines the first name and last name into a full name. This method then belongs to every object created with this constructor function. So you only have to write it once, not anew for each participant.
Step 4: Creating an Object with new
To create an object, you write let jan = new Person();. The keyword new calls the constructor and returns a new instance. You store this instance in the variable jan. Make sure the function name is capitalized. This is a common convention that makes the difference from normal functions clear to you.
Step 5: Setting Values and Outputting the Name
Next, you set the properties directly on the object. Write jan.vorname = "Jan"; and jan.nachname = "Bringmann";. With console.log(jan.getName());, you output the full name. After reloading the page or running the code, you will see the result in the console window. If you create a second person, you only repeat these assignments instead of defining the entire method again.
Step 6: Passing Values Directly via Parameters
The constructor can be simplified even further. Write a new function Teilnehmer and give it the parameters vorname and nachname. Inside the function, you assign the parameters to the properties, for example with this.vorname = vorname;. The constructor thus works just like a normal function with parameters, except that it returns an object.
Step 7: Calling the Constructor with Arguments
When calling it, you now pass the values directly in the parentheses. An example of this is let jan2 = new Teilnehmer("Jan", "Bringmann");. The first value ends up in the parameter vorname, the second in the parameter nachname. So you can create the object and fill it completely at the same time. Setting the values separately is no longer necessary, and your code becomes shorter and easier to understand.
Step 8: Getting to Know the new Object() Notation
Finally, it is worth looking at an alternative notation. An object can also be created with curly braces, for example let testobjekt = {};. let testobjekt = new Object(); works just as well, because it calls the generic Object constructor. Both variants produce an empty object with the same result. You will also encounter the concept of the constructor in many other programming languages such as Java, which is why understanding it is worthwhile.
Summary – Constructors in JavaScript: Easily Initialize Objects
A constructor prepares objects when they are created. There you define properties and methods that every new object receives, and you pass values directly via parameters. With new you create instances, and with this you access the current object inside the constructor. This makes your code reusable, clear, and easier to maintain. With this knowledge, you are well prepared to work on the practice exercises.
FAQ
What is a constructor?A constructor is a function that creates a new object and defines its basic structure.
What is the keyword new used for?With new you call the constructor. This creates a new instance that you can store in a variable.
What does this stand for in the constructor?This refers to the object that is currently being created. You use it to set properties and methods.
Can I pass values to a constructor?Yes, you can define parameters and pass values when calling it. You then assign these values to the properties.
Is new Object() the same as an object literal?Yes, both notations create an object with the same result. The curly braces are considered the shorter variant.
