Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript Working with Classes in JavaScript Review Working with Classes in JavaScript

A constructor method is Not required inside of a class.

Being as Constructor methods are not required inside of classes.

  1. Whats the point of this class then? (isn't a class a blueprint and the constructor a factory! if there is no factory why bother having a blueprint?!)?

  2. What is an example of a class without a constructor?

2 Answers

Sometimes you make classes without a constructor when using inheritance. In this case we don't want any object to be an instance of shape. But we don't want to rewrite the function toString every time when we make a new class for a shape. So Rectangle and Circle 'extend' the Shape class, this way they have access to the attributes and functions of the parent class. I hope that this answers your question :)

class Shape {
    toString () {
        return `Shape`
    }
}
class Rectangle extends Shape {
    constructor (width, height) {
      ...
    }
    toString () {
        return "Rectangle > " + super.toString()
    }
}
class Circle extends Shape {
    constructor (radius) {
      ...
    }
    toString () {
        return "Circle > " + super.toString()
    }
}
Steven Parker
Steven Parker
210,863 Points

A class might contain a number of data fields and/or methods, but nothing requiring any specific initialization. In that case, no explicit constructor is needed. But every instance still gets all the methods and fields.