-
-
Notifications
You must be signed in to change notification settings - Fork 627
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
Changes from 18 commits
f11a691
4db432d
2ac1058
5eb95e1
2d143a2
83004c7
cef0d63
c6ef884
29918bb
a57501a
fd51d19
58a8d5f
c3bd811
cdc38d8
6962ba4
904a2f7
34a142a
4439986
3b3d546
6193128
3daa0f1
7fc882a
df13d3a
9a2c6be
b10f08c
ed89b05
a927072
be06b65
616ba12
6966556
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
/bin/configlet | ||
/pnpm-lock.yaml | ||
/yarn.lock | ||
/.idea | ||
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": [] | ||
} |
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'); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// => 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'); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 |
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_). | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
```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'); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
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 |
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" | ||
} | ||
] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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