Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Object-Oriented JavaScript: Challenge Building Constructor Methods and Generating Objects createTokens() Method Solution

why does she use class instead of constructor function?

why not a regular object or a constructor function. I would imagine it would work too. Is it her preference or is there a some sort of benefit to using classes over the others?

2 Answers

Steven Parker
Steven Parker
231,236 Points

I think you meant object literal, and yes, both the constructor and object literal here create objects with the same contents. I'd guess classes are favored in the courses because they are a newer syntax and many students are eager to learn it.

Glad I could help, and happy holidays!   :christmas_tree:

Steven Parker
Steven Parker
231,236 Points

The constructor function is being used in the video example. Using "new" and the class is the way you invoke the constructor function.

I'm not sure what you mean by "regular object". I would think that a class instance is a "regular object". Please elaborate a bit if I'm missing the point.

I see, so if I am understanding this correctly, this would still be a function (or a function expression if you will):

const functionName = function() {
     this.properyName = "value";
};

But using "new" makes it into a constructor function:

const constructorFunctionName = new functionName();

With regular object I basically mean: const objectName = { propertyName: "value" } . But anyway base from what I learned, classes are syntax sugar and that is does not bring any new. I guess I was only curious why the tutor favoured classes so heavily.