Comp 101 Constructors

What is a constructor?

When we create a new object, its properties must be initialized before we can use it. For some objects, we:

  •  set the object's properties within the class itself
  • change its properties to fit our needs once the class has been created

 This approach is very similar to what we've seen in lecture with the point class:

// declaring the Point class
class Point {
    x: number = 0; // here, we are creating an x property for the Point class
                   // and initializing its value to 0
    y: number = 0; // we are doing the same thing here as above except with y
}
// when we create the point, its x and y properties are automatically 0
let point = new Point();
// we can change the values of x and y by directly accessing point's x and y properties
point.x = 101;
point.y = 110;

However, there is a way we can create our point so its properties are set to whatever we want when we first make our new object. We can do this by using a constructor!

A constructor is a special function written inside the class body that is called whenever we create a new object. We use it to initialize the properties of an object.

A constructor works like other functions in that:

  •  It can take it parameters
  • It will always return something
    • A constructor returns an object of the class 

How do we make a constructor?

The syntax of a constructor is as follows:

constructor(<paramaters>) {
    <initialize object properties here>
}

The parameters we take into our constructor are the values we want our object's properties to be. For example, if were were making a constructor for our Point class:

// note the name of the constructor is 'constructor' for all classes
constructor(x: number, y: number) {
// here, we're setting our object's x and y properties equal to the parameters of our constructor 
// we use the 'this' keyword to refer to the object being created   
    this.x = x; // "set the object being created's x property equal to x"
    this.y = y; // "set the object being created's y property to equal y"
}

this is a special keyword we use inside the constructor to refer to the object being created. When we declare our object in code outside of our class, we refer to it using the variable name we gave it, eg 

let point = new Point(); // in this case, our point object would be called point

However, since the variable name point has no meaning with the point class itself (remember our rules on scope!) our Point object needs a name our constructor can use within the class itself - and this name is this 

Another thing to keep in mind is that although in our example the parameters (x and y) and the properties of Point (x and y) appear to be the same thing, the variables are not in fact related! The scope of the constructor's parameters (x and y) is confined to the constructor, while the properties of the object (this.x and this.y) can be used anywhere in the class! Therefore, the names of our parameters do not have to match the names of our properties, making this a perfectly valid constructor for point:

constructor(coordinate1: number, coordiante2: number) {
    this.x = coordinate1;
    this.y = conordinate2;
}


How do we use a constructor?

Although constructors may seem unfamiliar, you have actually been using them since we started working with objects! - We call them whenever we create a new object!

let point = new Point();

In the example above, the () is actually a call to the Point constructor! In this specific example, the constructor does not have any parameters or may not be defined at all, in which case it just creates the object, without changing its properties.

However, imagine we had a constructor that looked like this:

constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
}

How would we create a new point object?

-By calling out constructor with parameters!

We can do this just like we'd call a normal function!

let point = new Point(101, 110); // here, new Point(101, 110) is a call to the Point constructor with 101 and 110 as parameters
print(point.x); // would print 101
print(point.y; // would print 110

Why use a constructor?

Say we wanted to create 100 point objects.

Without a constructor, this would take three lines of code for each

  • declaring the point object
  • initializing its x propertyinitializing its y property

Generally, programmers like avoiding repetition as much as possible, and with a constructor, we can combine those three lines into 1!

As our objects grow to have more properties, this becomes even more time-saving and useful!