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 trialYuval Blass
18,134 PointsSub-class | Frustrating
Can someone help me understand what is going on in this video? I understand the first video (Structure of class), but in this video, I got lost with all this information. I know that in order you guys will be able to help me, I need to be more specific, but I totally lost it.
5 Answers
Steven Parker
231,122 PointsSee if this helps:
Basically, a "sub-class" is a class. What makes it special is just that it shares properties and/or methods with another class (the "base" class). So when you define a sub-class, you don't have to start from scratch. By declaring that it "extends" the base class, you give all the properties and methods of that base class to begin with. Then you only have to define the differences and additions that apply just to it and not the base class.
If that doesn't clear it up, it would help if you could explain what you find confusing.
Steven Parker
231,122 PointsAfter changing the "=" to "{"...
// I put in this console command:
console.log(Rectangle.prototype.area)
// ...and I get this console output:
area() {
return this.height * this.width // So, this is a prototype, no?
}
Yuval Blass
18,134 PointsHi Steven,
1. So is it the same as I learned in object oriented javascript
when we use class.call(this, properties);
& sub-class.prototype = Object.create(class.prototype);
?
2. And by usings the classes syntax, How we define prototypes?
Thank you Steven,
You help me & other users in the community a lot...
Steven Parker
231,122 PointsThe new syntax gives you an alternative to the traditional method involving prototypes. Methods defined in the class (or subclass) serve the same purpose as those defined in the prototype in the traditional syntax. In the new syntax, the "prototype" is not used directly.
Yuval Blass
18,134 Pointsclass Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
area() {
return this.height * this.width // So, this is a prototype, no?
}
};