-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
126 additions
and
379 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"ltex.language": "en-GB" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,63 @@ | ||
# Inheritance | ||
|
||
Inheritance enables you to create a new class that is based on an existing | ||
class. The new class inherits the properties and methods of the existing class. | ||
This existing class is often referred to as the parent class, superclass, or | ||
base class, while the new class is known as the child class, subclass, or | ||
derived class. | ||
|
||
## Understanding Inheritance | ||
|
||
The primary benefit of inheritance is code reuse. You can define common | ||
functionality in a parent class and then extend this class to create child | ||
classes that inherit this functionality, adding or overriding properties and | ||
methods as needed. | ||
|
||
## Example Scenario | ||
|
||
Let's continue with our smart home theme. Imagine we have a general | ||
`SmartDevice` class that defines properties and methods common to all smart | ||
devices, like connectivity status or device name. We can then create more | ||
specific device classes (like `SmartLight` or `SmartCamera`) that inherit from | ||
`SmartDevice` and add their unique features. | ||
<Vimeo id="933310880" /> | ||
|
||
## Creating a parent class | ||
|
||
First, we define our parent class, `SmartDevice`, with some basic properties and | ||
methods. | ||
If we want to create a set of classes that share some common functionality, we | ||
should create a parent class. This parent class will contain the shared | ||
functionality, and the child classes will inherit from it. | ||
|
||
```js | ||
class SmartDevice { | ||
constructor(name) { | ||
this.name = name | ||
this.isConnected = false | ||
constructor() { | ||
this.isOn = false | ||
} | ||
|
||
connect() { | ||
this.isConnected = true | ||
console.log(`${this.name} is now connected.`) | ||
togglePower() { | ||
this.isOn = !this.isOn | ||
} | ||
} | ||
``` | ||
|
||
We don't intend to use this class to directly create objects, but we'll use it | ||
as a starting point to build our more specific classes. | ||
|
||
## Inheriting from a parent class | ||
|
||
Next, we create a child class that inherits from `SmartDevice`. We use the | ||
`extends` keyword for inheritance in JavaScript. | ||
To inherit from a parent class, we use the `extends` keyword, and then call the | ||
`super()` method in the child class's constructor. | ||
|
||
An important step is calling the special `super()` function inside the child's | ||
`constructor()`. The `super()` function calls the parent's constructor, which is | ||
necessary to set up shared properties like `name` and `isConnected`. | ||
::: info | ||
|
||
::: code-group | ||
The `super()` method calls the parent class's constructor, passing in any | ||
arguments that are needed for the parent class's constructor. | ||
|
||
```js | ||
class SmartLight extends SmartDevice { | ||
constructor(name, color) { | ||
super(name) // Calls parent's constructor with the name parameter | ||
this.color = color | ||
} | ||
::: | ||
|
||
::: code-group | ||
|
||
changeColor(newColor) { | ||
this.color = newColor | ||
console.log(`${this.name}'s color changed to ${this.color}.`) | ||
```js {1,3} | ||
class SmartCamera extends SmartDevice { | ||
constructor(location) { | ||
super() | ||
this.location = location | ||
this.batteryLife = 100 | ||
} | ||
} | ||
|
||
const studyLight = new SmartLight('Epic Lamp', 'yellow') | ||
console.log(studyLight) | ||
|
||
// use the inherited .connect() method | ||
studyLight.connect() | ||
|
||
// use the child-specific .changeColor() method | ||
studyLight.changeColor('ochre') | ||
const poolCam = new SmartCamera('Pool House') | ||
poolCam.togglePower() | ||
console.log(poolCam) | ||
``` | ||
|
||
```console [output] | ||
SmartLight { name: 'Epic Lamp', isConnected: false, color: 'yellow' } | ||
Epic Lamp is now connected. | ||
Epic Lamp's color changed to ochre. | ||
SmartCamera { | ||
isOn: true, | ||
batteryLife: 100, | ||
location: 'Pool House' | ||
} | ||
``` | ||
|
||
::: | ||
|
||
Because we have used `extends` and `super()`, we now have access to all | ||
`SmartDevice` functionality on our `SmartLight`. Neat! | ||
|
||
The next step would be to create a `SmartCamera` class which also inherits from | ||
`SmartDevice`. Each child class doesn't need to implement things like `name` and | ||
`connect()`, meaning we do not repeat and have to maintain all the repeated | ||
code. | ||
Notice that the `SmartCamera` class has access to the `togglePower()` method and | ||
the `isOn` property, even though we didn't define them in the `SmartCamera` | ||
class. This is because `SmartCamera` inherits from `SmartDevice`. |
Oops, something went wrong.