You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add a Super section
Will add a `extends` section soon, to closembeaudru#49.
* Add reference for `super`
* Add an `Extends` section
* Merged with current master and improved typos/notion
The `extends` keyword is used in class declarations or class expressions to create a class which is a child of another class ([Ref: MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/extends)). The subclass inherits all the properties of the superclass and additionally can add new properties or modify the inherited ones.
1269
+
1270
+
The `super` keyword is used to call functions on an object's parent, including its constructor.
1271
+
1272
+
-`super` keyword must be used before the `this` keyword is used in constructor
1273
+
- Invoking `super()` calls the parent class constructor. If you want to pass some arguments in a class's constructor to its parent's constructor, you call it with `super(arguments)`.
1274
+
- If the parent class have a method (even static) called `X`, you can use `super.X()` to call it in a child class.
1275
+
1276
+
#### Sample Code
1277
+
1278
+
```js
1279
+
classPolygon {
1280
+
constructor(height, width) {
1281
+
this.name='Polygon';
1282
+
this.height= height;
1283
+
this.width= width;
1284
+
}
1285
+
1286
+
getHelloPhrase() {
1287
+
return`Hi, I am a ${this.name}`;
1288
+
}
1289
+
}
1290
+
1291
+
classSquareextendsPolygon {
1292
+
constructor(length) {
1293
+
// Here, it calls the parent class' constructor with lengths
1294
+
// provided for the Polygon's width and height
1295
+
super(length, length);
1296
+
// Note: In derived classes, super() must be called before you
1297
+
// can use 'this'. Leaving this out will cause a reference error.
1298
+
this.name='Square';
1299
+
this.length= length;
1300
+
}
1301
+
1302
+
getCustomHelloPhrase() {
1303
+
constpolygonPhrase=super.getHelloPhrase(); // accessing parent method with super.X() syntax
1304
+
return`${polygonPhrase} with a lenght of ${this.length}`;
1305
+
}
1306
+
1307
+
getarea() {
1308
+
returnthis.height*this.width;
1309
+
}
1310
+
}
1311
+
1312
+
constmySquare=newSquare(10);
1313
+
console.log(mySquare.area) // 100
1314
+
console.log(mySquare.getHelloPhrase()) // 'Hi, I am a Square' -- Square inherits from Polygon and has access to its methods
1315
+
console.log(mySquare.getCustomHelloPhrase()) // 'Hi, I am a Square with a lenght of 10'
1316
+
```
1317
+
1318
+
**Note :** If we had tried to use `this` before calling `super()` in Square class, a ReferenceError would have been raised:
1319
+
1320
+
```js
1321
+
classSquareextendsPolygon {
1322
+
constructor(length) {
1323
+
this.height; // ReferenceError, super needs to be called first!
1324
+
1325
+
// Here, it calls the parent class' constructor with lengths
1326
+
// provided for the Polygon's width and height
1327
+
super(length, length);
1328
+
1329
+
// Note: In derived classes, super() must be called before you
1330
+
// can use 'this'. Leaving this out will cause a reference error.
0 commit comments