Skip to content

Commit 9dd938d

Browse files
committed
updated
1 parent 2992e82 commit 9dd938d

8 files changed

+114
-103
lines changed

Object Oriented Programming/15 Understand Where an Object’s Prototype Comes From.js

-21
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Just like people inherit genes from their parents, an object inherits its prototype directly from the constructor function that created it. For example, here the `Bird` constructor creates the `duck` object:
2+
```js
3+
function Bird(name) {
4+
this.name = name;
5+
}
6+
7+
let duck = new Bird("Donald");
8+
```
9+
duck inherits its prototype from the Bird constructor function. You can show this relationship with the `isPrototypeOf` method:
10+
```js
11+
Bird.prototype.isPrototypeOf(duck);
12+
// returns true
13+
```
14+
Use `isPrototypeOf` to check the prototype of beagle.
15+
```js
16+
function Dog(name) {
17+
this.name = name;
18+
}
19+
20+
let beagle = new Dog("Snoopy");
21+
22+
// Add your code below this line
23+
Dog.prototype.isPrototypeOf(beagle);

Object Oriented Programming/16 Understand the Prototype Chain.js

-25
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
All objects in JavaScript (with a few exceptions) have a prototype.
2+
Also, an object’s prototype itself is an object.
3+
```js
4+
function Bird(name) {
5+
this.name = name;
6+
}
7+
8+
typeof Bird.prototype; // => object
9+
```
10+
Because a `prototype` is an object, a prototype can have its own prototype!
11+
In this case, the prototype of `Bird.prototype` is `Object.prototype`:
12+
```js
13+
Object.prototype.isPrototypeOf(Bird.prototype);
14+
// returns true
15+
// How is this useful? You may recall the hasOwnProperty method from a previous challenge:
16+
17+
let duck = new Bird("Donald");
18+
duck.hasOwnProperty("name"); // => true
19+
```
20+
The hasOwnProperty method is defined in Object.prototype, which can be accessed by `Bird.prototype`, which can then be accessed by duck.
21+
This is an example of the prototype chain.
22+
23+
In this prototype chain, `Bird` is the `supertype` for `duck`, while `duck` is the `subtype`.
24+
`Object` is a `supertype` for both `Bird` and `duck`.
25+
26+
`Object` is a `supertype` for `all objects` in `JavaScript`.
27+
Therefore, any object can use the `hasOwnProperty` method.

Object Oriented Programming/17 Use Inheritance So You Don't Repeat Yourself.js renamed to Object Oriented Programming/17 Use Inheritance So You Don't Repeat Yourself.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/*There's a principle in programming called Don't Repeat Yourself (DRY).
1+
There's a principle in programming called `Don't Repeat Yourself (DRY).`
22
The reason repeated code is a problem is because any change requires fixing code in multiple places.
33
This usually means more work for programmers and more room for errors.
44

5-
Notice in the example below that the describe method is shared by Bird and Dog:
6-
5+
Notice in the example below that the describe method is shared by `Bird` and `Dog`:
6+
```js
77
Bird.prototype = {
88
constructor: Bird,
99
describe: function() {
@@ -17,9 +17,10 @@ Dog.prototype = {
1717
console.log("My name is " + this.name);
1818
}
1919
};
20+
```
2021
The describe method is repeated in two places.
21-
The code can be edited to follow the DRY principle by creating a supertype (or parent) called Animal:
22-
22+
The code can be edited to follow the `DRY` principle by creating a `supertype` (or parent) called Animal:
23+
```js
2324
function Animal() { };
2425

2526
Animal.prototype = {
@@ -28,18 +29,20 @@ Animal.prototype = {
2829
console.log("My name is " + this.name);
2930
}
3031
};
31-
Since Animal includes the describe method, you can remove it from Bird and Dog:
32-
32+
```
33+
Since `Animal` includes the describe method, you can remove it from `Bird` and `Dog`:
34+
```js
3335
Bird.prototype = {
3436
constructor: Bird
3537
};
3638

3739
Dog.prototype = {
3840
constructor: Dog
3941
};
40-
41-
The eat method is repeated in both Cat and Bear.
42-
Edit the code in the spirit of DRY by moving the eat method to the Animal supertype.*/
42+
```
43+
The eat method is repeated in both `Cat` and `Bear`.
44+
Edit the code in the spirit of `DRY` by moving the eat method to the `Animal` `supertype`.
45+
```js
4346
function Cat(name) {
4447
this.name = name;
4548
}

Object Oriented Programming/18 Inherit Behaviors from a Supertype.js

-39
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
In the previous challenge, you created a `supertype` called `Animal` that defined behaviors shared by all animals:
2+
```js
3+
function Animal() { }
4+
Animal.prototype.eat = function() {
5+
console.log("nom nom nom");
6+
};
7+
```
8+
This and the next challenge will cover how to reuse Animal's methods inside `Bird` and `Dog` without defining them again. It uses a technique called `inheritance`.
9+
10+
This challenge covers the first step: make an instance of the `supertype` (or parent).
11+
12+
You already know one way to create an instance of `Animal` using the new operator:
13+
```js
14+
let animal = new Animal();
15+
```
16+
There are some `disadvantages` when using this syntax for inheritance, which are too complex for the scope of this challenge. Instead, here's an alternative approach without those disadvantages:
17+
```js
18+
let animal = Object.create(Animal.prototype);
19+
```
20+
`Object.create(obj)` creates a new object, and sets obj as the new object's prototype. Recall that the prototype is like the "recipe" for creating an object. By setting the prototype of animal to be `Animal's` prototype, you are effectively giving the animal instance the same "recipe" as any other instance of `Animal`.
21+
```js
22+
animal.eat(); // prints "nom nom nom"
23+
animal instanceof Animal; // => true
24+
```
25+
Use `Object.create` to make two instances of `Animal` named `duck` and `beagle`.*/
26+
```js
27+
function Animal() { }
28+
29+
Animal.prototype = {
30+
constructor: Animal,
31+
eat: function() {
32+
console.log("nom nom nom");
33+
}
34+
};
35+
36+
// Add your code below this line
37+
38+
let duck = Object.create(Animal.prototype); // Change this line
39+
let beagle = Object.create(Animal.prototype); // Change this line
40+
41+
duck.eat(); // Should print "nom nom nom"
42+
beagle.eat(); // Should print "nom nom nom"

Object Oriented Programming/19 Set the Child's Prototype to an Instance of the Parent.js renamed to Object Oriented Programming/19 Set the Child's Prototype to an Instance of the Parent.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
/*In the previous challenge you saw the first step for inheriting behavior from the supertype (or parent) Animal: making a new instance of Animal.
2-
3-
This challenge covers the next step: set the prototype of the subtype (or child)—in this case, Bird—to be an instance of Animal.
1+
In the previous challenge you saw the first step for inheriting behavior from the `supertype` (or parent) Animal: making a new instance of `Animal`.
42

3+
This challenge covers the next step: set the prototype of the subtype (or child)—in this case, `Bird—to` be an instance of `Animal`.
4+
```js
55
Bird.prototype = Object.create(Animal.prototype);
6-
Remember that the prototype is like the "recipe" for creating an object. In a way, the recipe for Bird now includes all the key "ingredients" from Animal.
7-
6+
```
7+
**Remember that the prototype is like the "recipe" for creating an object**. In a way, the recipe for Bird now includes all the key "ingredients" from `Animal`.
8+
```js
89
let duck = new Bird("Donald");
910
duck.eat(); // prints "nom nom nom"
1011
duck inherits all of Animal's properties, including the eat method.
1112
12-
13-
Modify the code so that instances of Dog inherit from Animal.*/
14-
13+
```
14+
Modify the code so that instances of `Dog` inherit from `Animal`.
15+
```js
1516
function Animal() { }
1617
1718
Animal.prototype = {

0 commit comments

Comments
 (0)