You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 51 - Prototypal Inheritance Review.md
+28-28Lines changed: 28 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -5,10 +5,10 @@ What I wanted to do in this video is do a quick review of how prototypal inherit
5
5
What I want to do here is let's make a dog function that will create puppies or create many dogs:
6
6
7
7
```js
8
-
functionDog(name, breed) {
9
-
this.name= name;
10
-
this.breed= breed;
11
-
}
8
+
functionDog(name, breed) {
9
+
this.name= name;
10
+
this.breed= breed;
11
+
}
12
12
```
13
13
Notice we're using a capital D on `Dog` because this is what's called a constructor function. The function itself will pass in whatever we get for name and breed are assigned as `name`, and `breed`.
14
14
@@ -41,14 +41,14 @@ Where did they come from? Well, we have the "Mama array", which is capital A, `A
41
41
We're going to take a look at how they don't just take their own, they actually share the same one. Let's add a prototype method to our `Dog`:
42
42
43
43
```js
44
-
functionDog(name, breed) {
45
-
this.name= name;
46
-
this.breed= breed;
47
-
}
48
-
Dog.prototype.bark=function() {
49
-
console.log(`Bark Bark! My name is ${this.name}`);
50
-
}
51
-
constsnickers=newDog('Snickers', 'King Charles');
44
+
functionDog(name, breed) {
45
+
this.name= name;
46
+
this.breed= breed;
47
+
}
48
+
Dog.prototype.bark=function() {
49
+
console.log(`Bark Bark! My name is ${this.name}`);
50
+
}
51
+
constsnickers=newDog('Snickers', 'King Charles');
52
52
```
53
53
54
54
Remember, we're using template strings to call `this.name`, because we want to reference the name of the actual dog. If you run `snickers.bark()` in your console, it says, "Bark, bark. My name is Snickers." Makes sense.
@@ -63,26 +63,26 @@ If we run `sunny.bark()`, It returns "Bark, bark. My name is Sunny." You see how
63
63
What's really cool about that is you can still change it after the fact:
64
64
65
65
```js
66
-
functionDog(name, breed) {
67
-
this.name= name;
68
-
this.breed= breed;
69
-
}
70
-
Dog.prototype.bark=function() {
71
-
console.log(`Bark Bark! My name is ${this.name}`);
72
-
}
73
-
constsnickers=newDog('Snickers', 'King Charles');
74
-
constsunny=newDog('Sunny', 'Golden Doodle');
75
-
76
-
Dog.prototype.bark=function() {
77
-
console.log(`Bark bark! my name is ${this.name} and I'm a ${this.breed}!`);
78
-
}
66
+
functionDog(name, breed) {
67
+
this.name= name;
68
+
this.breed= breed;
69
+
}
70
+
Dog.prototype.bark=function() {
71
+
console.log(`Bark Bark! My name is ${this.name}`);
72
+
}
73
+
constsnickers=newDog('Snickers', 'King Charles');
74
+
constsunny=newDog('Sunny', 'Golden Doodle');
75
+
76
+
Dog.prototype.bark=function() {
77
+
console.log(`Bark bark! my name is ${this.name} and I'm a ${this.breed}!`);
78
+
}
79
79
```
80
80
81
81
Even though I've created `snickers` before I've added this method should it still do this? Absolutely. What if I added a second method onto it after the fact?
82
82
83
83
```js
84
-
Dog.prototype.cuddle=function() {
85
-
console.log(`I love you owner!`);
84
+
Dog.prototype.cuddle=function() {
85
+
console.log(`I love you owner!`);
86
86
}
87
87
```
88
88
@@ -92,4 +92,4 @@ If you just `snickers` in your console and you open it up and you won't see `bar
92
92
93
93
But `bark` and `cuddle` are on the prototype, which means that they're not part of the actual object, `snickers`. But if you open that it up, you'll see `bark` and `cuddle` are right inside of `__proto__` in your DevTools.
94
94
95
-
With that in mind, let's learn about how we can also do this sort of thing when we write classes.
95
+
With that in mind, let's learn about how we can also do this sort of thing when we write classes.
0 commit comments