JavaScript Introducing ES2015 Classes Sub-Classes

Sub-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
Steven Parker
171,120 Points

See 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
Steven Parker
171,120 Points

After 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?
 }

Hi 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...

Faye Bridge | Steven Parker

Steven Parker
Steven Parker
171,120 Points

The 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.

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  area() {
    return this.height * this.width // So, this is a prototype, no?
   } 
};

Thank you!