Skip to content

Commit 9315477

Browse files
Mohamed3onmbeaudru
authored andcommitted
Add a section for super and extends (mbeaudru#71)
* Add a Super section Will add a `extends` section soon, to close mbeaudru#49. * Add reference for `super` * Add an `Extends` section * Merged with current master and improved typos/notion
1 parent 9506ac8 commit 9315477

File tree

1 file changed

+84
-5
lines changed

1 file changed

+84
-5
lines changed

README.md

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,23 @@ When you struggle to understand a notion, I suggest you look for answers on the
9090
+ [Class](#class)
9191
- [Samples](#samples)
9292
- [External resources](#external-resources-7)
93+
+ [Extends and super keywords](#extends-and-super-keywords)
94+
- [Sample Code](#sample-code-6)
95+
- [External Resources](#external-resources-8)
9396
+ [Async Await](#async-await)
94-
- [Sample code](#sample-code-6)
97+
- [Sample code](#sample-code-7)
9598
- [Explanation with sample code](#explanation-with-sample-code-2)
9699
- [Error handling](#error-handling)
97-
- [External resources](#external-resources-8)
98-
+ [Truthy / Falsy](#truthy--falsy)
99100
- [External resources](#external-resources-9)
101+
+ [Truthy / Falsy](#truthy--falsy)
102+
- [External resources](#external-resources-10)
100103
+ [Static Methods](#static-methods)
101104
- [Short Explanation](#short-explanation-1)
102-
- [Sample Code](#sample-code-7)
105+
- [Sample Code](#sample-code-8)
103106
- [Detailed Explanation](#detailed-explanation-2)
104107
* [Calling other static methods from a static method](#calling-other-static-methods-from-a-static-method)
105108
* [Calling static methods from non-static methods](#calling-static-methods-from-non-static-methods)
106-
- [External resources](#external-resources-10)
109+
- [External resources](#external-resources-11)
107110
* [Glossary](#glossary)
108111
+ [Scope](#-scope)
109112
+ [Variable mutation](#-variable-mutation)
@@ -1260,6 +1263,82 @@ For classes understanding:
12601263
- [ES6 Features - Classes](http://es6-features.org/#ClassDefinition)
12611264
- [JavaScript Classes - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes)
12621265

1266+
### `Extends` and `super` keywords
1267+
1268+
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+
class Polygon {
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+
class Square extends Polygon {
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+
const polygonPhrase = super.getHelloPhrase(); // accessing parent method with super.X() syntax
1304+
return `${polygonPhrase} with a lenght of ${this.length}`;
1305+
}
1306+
1307+
get area() {
1308+
return this.height * this.width;
1309+
}
1310+
}
1311+
1312+
const mySquare = new Square(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+
class Square extends Polygon {
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.
1331+
this.name = 'Square';
1332+
}
1333+
}
1334+
```
1335+
1336+
#### External Resources
1337+
1338+
- [Extends - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/extends)
1339+
- [Super operator - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super)
1340+
- [Inheritance - MDN](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance)
1341+
12631342
### Async Await
12641343

12651344
In addition to [Promises](#promises), there is a new syntax you might encounter to handle asynchronous code named *async / await*.

0 commit comments

Comments
 (0)