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

Matthew Francis
Matthew Francis
6,967 Points

Purpose of classes?

Why not just do the usual way and create a function constructor? what benefits do classes give?

For example,

//Object
var human = function(legNum){
  this.legs = legNum;
}

var george = new human(2);
console.log(george.legs);

//Class
class human2 {
  constuctor(legNum){
    this.legs = legNum;
  }
}
var matt = new human(2);
console.log(matt.legs);

1 Answer

Steven Parker
Steven Parker
229,644 Points

There might not be any significant benefit.

I've been watching videos on youTube of lectures by Douglas Crockford, author and major contributor to the evolution of JavaScript. And apparently classes are one recent addition to the language that he's not favorably impressed with.

Watch the lectures yourself to get the direct scoop, but I got the impression that classes were only added to the language to appease people who are familiar with the class object model from other languages and are not comfortable with the traditional prototypal object model of JavaScript. I'm not sure classes offer any significant benefit to developers who are comfortable with the prototypal model.

It's still important to understand class and related features so that you can comprehend and maintain code written by others when required. But you might not find any reason to use it in your own code.