Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 1341 inheritance concept #1447

Merged
merged 30 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
f11a691
add .idea directory to gitignore
jakewitcher Oct 10, 2021
4db432d
init commit
jakewitcher Oct 16, 2021
2ac1058
update design.md and global config.json with inheritance concept
jakewitcher Oct 16, 2021
5eb95e1
edit text, fix errors in examples
jakewitcher Oct 16, 2021
2d143a2
add .idea directory to gitignore
jakewitcher Oct 10, 2021
83004c7
init commit
jakewitcher Oct 16, 2021
cef0d63
update design.md and global config.json with inheritance concept
jakewitcher Oct 16, 2021
c6ef884
edit text, fix errors in examples
jakewitcher Oct 16, 2021
29918bb
Merge branch 'issue-1341-inheritance-concept' of github.com:jakewitch…
jakewitcher Oct 16, 2021
a57501a
edit about, add section on prototype inheritance, reduce content in i…
jakewitcher Oct 16, 2021
fd51d19
edit text in about and introduction for inheritance concept
jakewitcher Oct 17, 2021
58a8d5f
edit errors concept and factory-sensors introduction
jakewitcher Oct 17, 2021
c3bd811
edit section on inheritance and the prototype chain in about and intr…
jakewitcher Oct 17, 2021
cdc38d8
add inheritance section to factory-sensors introduction, match change…
jakewitcher Oct 17, 2021
6962ba4
remove hard wrap from md lines, each sentence placed on its own line
jakewitcher Oct 17, 2021
904a2f7
rephrase line in Errors docs on using inheritance to create custom er…
jakewitcher Oct 17, 2021
34a142a
mention the extends keyword to text on creating custom errors
jakewitcher Oct 17, 2021
4439986
[CI] Format code
github-actions[bot] Oct 17, 2021
3b3d546
group related sentences in inheritance about.md
jakewitcher Oct 18, 2021
6193128
group related sentences in inheritance about.md
jakewitcher Oct 18, 2021
3daa0f1
replace let with const
jakewitcher Oct 18, 2021
7fc882a
replace let with const
jakewitcher Oct 18, 2021
df13d3a
group related sentences in inheritance introduction.md
jakewitcher Oct 18, 2021
9a2c6be
replace let with const
jakewitcher Oct 18, 2021
b10f08c
update Error class reference to be inline code
jakewitcher Oct 18, 2021
ed89b05
change let to const
jakewitcher Oct 18, 2021
a927072
group related sentences in factory-sensors exercise introduction.md
jakewitcher Oct 18, 2021
be06b65
use const in all inheritance examples, group related sentences across…
jakewitcher Oct 18, 2021
616ba12
remove ide directory from gitignore
jakewitcher Oct 18, 2021
6966556
add inheritance slug to top-level concepts array
jakewitcher Oct 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/bin/configlet
/pnpm-lock.yaml
/yarn.lock
/.idea
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a global gitignore file on your local machine for excluding editor/IDE related folders. We don't want this file to grow with each new editor a contributor brings along.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed from the project .gitignore and added to global .gitignore

4 changes: 3 additions & 1 deletion concepts/errors/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ try {
}
```

As any object in JavaScript the Error can be "extended" to create Custom errors. You can use the `instanceof` syntax to check if the error caught is an instance of a particular object.
As with any class in JavaScript, subclasses can inherit from `Error` to create Custom errors by using the `extends` keyword.

The `instanceof` syntax will check if the error caught is an instance of a particular subclass of `Error`.

```javascript
class CustomError extends Error {}
Expand Down
6 changes: 4 additions & 2 deletions concepts/errors/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Using the `throw` syntax, you can throw an Error.
throw new Error('Oops');
```

When an Error is thrown, the current execution is stopped and resume in the first catch block of the call stack.
When an Error is thrown, the current execution is stopped and resumes in the first catch block of the call stack.
jakewitcher marked this conversation as resolved.
Show resolved Hide resolved

```javascript
try {
Expand All @@ -30,7 +30,9 @@ try {
}
```

As any object in JavaScript the Error can be "extended" to create Custom errors. You can use the `instanceof` syntax to check if the error caught is an instance of a particular object.
As with any class in JavaScript, subclasses can inherit from `Error` to create Custom errors by using the `extends` keyword.

The `instanceof` syntax will check if the error caught is an instance of a particular subclass of `Error`.

```javascript
class CustomError extends Error {}
Expand Down
5 changes: 5 additions & 0 deletions concepts/inheritance/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"blurb": "Inheritance creates parent-child relationships between classes in JavaScript. Learn how to use inheritance to create objects with shared behavior.",
"authors": ["JakeWitcher"],
"contributors": []
}
147 changes: 147 additions & 0 deletions concepts/inheritance/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# About

Inheritance is a way to create parent-child relationships between classes.

The child class (sometimes referred to as a _subclass_) has access to the behavior and data defined by the parent class (sometimes referred to as a _superclass_).
jakewitcher marked this conversation as resolved.
Show resolved Hide resolved

```javascript
class Pet {
constructor(name) {
this.name = name;
}

introduce() {
console.log(`This is my pet, ${this.name}.`);
}
}

class Dog extends Pet {}

let dog = new Dog('Otis');
jakewitcher marked this conversation as resolved.
Show resolved Hide resolved
dog.introduce();
// => This is my pet, Otis.
```

The `extends` keyword in the child class declaration establishes a relationship with the parent class through the [prototype chain][prototype-chain].

Objects created by the child's constructor will have the parent class's prototype in their prototype chain, providing access to any methods or data defined by the parent.

```javascript
let dog = new Dog('Otis');

Dog.prototype.isPrototypeOf(dog); // => true
Pet.prototype.isPrototypeOf(dog); // => true
Pet.prototype.isPrototypeOf(Dog.prototype); // => true

Pet.prototype.hasOwnProperty('introduce'); // => true
Dog.prototype.hasOwnProperty('introduce'); // => false
dog.hasOwnProperty('introduce'); // => false
```

## Constructors

If no constructor function is defined by the child class, the parent constructor function is used.

However, if the child class defines a constructor function of its own, the parent constructor must be explicitly called.

To invoke the parent constructor from within the child constructor's scope, the keyword `super` is used.
jakewitcher marked this conversation as resolved.
Show resolved Hide resolved

```javascript
class Pet {
constructor(name) {
this.name = name;
}
}

class Dog extends Pet {
constructor(name, breed) {
super(name);
this.breed = breed;
}
}

let dog = new Dog('Otis', 'Pug');
```

Because the parent constructor is responsible for initializing a new object and assigning it to `this`, it must be called before `this` is used by the child constructor.

```javascript
class Dog extends Pet {
constructor(name, breed) {
// using 'this' before calling the parent constructor with 'super'
this.breed = breed;
super(name);
}
}

let dog = new Dog('Otis', 'Pug');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let dog = new Dog('Otis', 'Pug');
const dog = new Dog('Otis', 'Pug');

// => ReferenceError: Must call super constructor in derived class before accessing 'this'...
```

## Defining Methods on the Child Class

A child class may define behavior of its own in addition to the behavior inherited from the parent.

This is one of the key reasons for using inheritance; to have specialized child classes with their own unique data and methods that are related through shared methods and data supplied by the parent class.

```javascript
class Dog extends Pet {
constructor(name, breed) {
super(name);
this.breed = breed;
}

describe() {
console.log(`${this.name} is a ${this.breed}.`);
}
}

let dog = new Dog('Otis', 'Pug');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let dog = new Dog('Otis', 'Pug');
const dog = new Dog('Otis', 'Pug');

dog.introduce();
dog.describe();
// => 'This is my pet, Otis.'
// => 'Otis is a Pug.'
```

## Overriding Methods Inherited From the Parent Class

A child class can also override the behavior of a method defined by the parent and replace or extend it with behavior defined by the child class.

```javascript
class Cat extends Pet {
// replacing parent class behavior
introduce() {
console.log(`This is my cat, ${this.name}.`);
}
}

class Dog extends Pet {
constructor(name, breed) {
super(name);
this.breed = breed;
}

describe() {
console.log(`${this.name} is a ${this.breed}.`);
}

// extending parent class behavior
introduce() {
super.introduce();
this.describe();
}
}

let cat = new Cat('Milo');
cat.introduce();
// => 'This is my cat, Milo.'

let dog = new Dog('Otis', 'Pug');
dog.introduce();
// => This is my pet, Otis.
// => Otis is a Pug.
```

To call a method defined on the parent class from the body of a method with the same name on the child class, the keyword `super` must be used to reference the parent.

[prototype-chain]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain
41 changes: 41 additions & 0 deletions concepts/inheritance/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Introduction

Inheritance is a way to create parent-child relationships between classes.

The child class (sometimes referred to as a _subclass_) has access to the behavior and data defined by the parent class (sometimes referred to as a _superclass_).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Inheritance is a way to create parent-child relationships between classes.
The child class (sometimes referred to as a _subclass_) has access to the behavior and data defined by the parent class (sometimes referred to as a _superclass_).
Inheritance is a way to create parent-child relationships between classes.
The child class (sometimes referred to as a _subclass_) has access to the behavior and data defined by the parent class (sometimes referred to as a _superclass_).


```javascript
class Pet {
constructor(name) {
this.name = name;
}

introduce() {
console.log(`This is my pet, ${this.name}.`);
}
}

class Dog extends Pet {}

let dog = new Dog('Otis');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let dog = new Dog('Otis');
const dog = new Dog('Otis');

dog.introduce();
// => This is my pet, Otis.
```

The `extends` keyword in the child class declaration establishes a relationship with the parent class through the [prototype chain][prototype-chain].

Objects created by the child's constructor will have the parent class's prototype in their prototype chain, providing access to any methods or data defined by the parent.

```javascript
let dog = new Dog('Otis');
jakewitcher marked this conversation as resolved.
Show resolved Hide resolved

Dog.prototype.isPrototypeOf(dog); // => true
Pet.prototype.isPrototypeOf(dog); // => true
Pet.prototype.isPrototypeOf(Dog.prototype); // => true

Pet.prototype.hasOwnProperty('introduce'); // => true
Dog.prototype.hasOwnProperty('introduce'); // => false
dog.hasOwnProperty('introduce'); // => false
```

[prototype-chain]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain
14 changes: 14 additions & 0 deletions concepts/inheritance/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"url": "https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance",
"description": "MDN: Inheritance"
},
{
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain",
"description": "MDN: Inheritance and the Prototype Chain"
},
{
"url": "https://javascript.info/class-inheritance",
"description": "javascript.info: Class Inheritance"
}
]
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
"slug": "factory-sensors",
"name": "Factory Sensors",
"uuid": "2ccafa38-2802-44c1-8758-7415edefa909",
"concepts": ["errors"],
"concepts": ["errors", "inheritance"],
"prerequisites": ["null-undefined", "conditionals"],
"status": "beta"
},
Expand Down
50 changes: 48 additions & 2 deletions exercises/concept/factory-sensors/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Introduction

## Errors

Errors are useful to report when something is wrong or unexpected in a program or a piece of code.

They are javascript objects.
Expand All @@ -19,7 +21,7 @@ Using the `throw` syntax, you can throw an Error.
throw new Error('Oops');
```

When an Error is thrown, the current execution is stopped and resume in the first catch block of the call stack.
When an Error is thrown, the current execution is stopped and resumes in the first catch block of the call stack.

```javascript
try {
Expand All @@ -30,7 +32,49 @@ try {
}
```

As any object in JavaScript the Error can be "extended" to create Custom errors. You can use the `instanceof` syntax to check if the error caught is an instance of a particular object.
## Inheritance

Inheritance is a way to create parent-child relationships between classes.

The child class (sometimes referred to as a _subclass_) has access to the behavior and data defined by the parent class (sometimes referred to as a _superclass_).
jakewitcher marked this conversation as resolved.
Show resolved Hide resolved

```javascript
class Pet {
constructor(name) {
this.name = name;
}

introduce() {
console.log(`This is my pet, ${this.name}.`);
}
}

class Dog extends Pet {}

let dog = new Dog('Otis');
jakewitcher marked this conversation as resolved.
Show resolved Hide resolved
dog.introduce();
// => This is my pet, Otis.
```

The `extends` keyword in the child class declaration establishes a relationship with the parent class through the [prototype chain][prototype-chain].

Objects created by the child's constructor will have the parent class's prototype in their prototype chain, providing access to any methods or data defined by the parent.

```javascript
let dog = new Dog('Otis');
jakewitcher marked this conversation as resolved.
Show resolved Hide resolved

Dog.prototype.isPrototypeOf(dog); // => true
Pet.prototype.isPrototypeOf(dog); // => true
Pet.prototype.isPrototypeOf(Dog.prototype); // => true

Pet.prototype.hasOwnProperty('introduce'); // => true
Dog.prototype.hasOwnProperty('introduce'); // => false
dog.hasOwnProperty('introduce'); // => false
```

As with any class in JavaScript, subclasses can inherit from `Error` to create Custom errors by using the `extends` keyword.

The `instanceof` syntax will check if the error caught is an instance of a particular subclass of `Error`.
jakewitcher marked this conversation as resolved.
Show resolved Hide resolved

```javascript
class CustomError extends Error {}
Expand All @@ -43,3 +87,5 @@ try {
}
}
```

[prototype-chain]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain
1 change: 1 addition & 0 deletions exercises/concept/factory-sensors/.meta/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The goal of this exercise is to teach the student how to handle errors / excepti
## Concepts

- `errors`
- `inheritance`

## Prerequisites

Expand Down