Skip to content

Commit 1c7755b

Browse files
authored
Fix code example
The previous code returns `NaN` instead of `Lydia Hallie`
1 parent 67d1764 commit 1c7755b

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,9 @@ function Person(firstName, lastName) {
334334
}
335335

336336
const member = new Person("Lydia", "Hallie");
337-
Person.getFullName = () => this.firstName + this.lastName;
337+
Person.getFullName = function () {
338+
return `${this.firstName} ${this.lastName}`;
339+
}
338340

339341
console.log(member.getFullName());
340342
```
@@ -351,7 +353,11 @@ console.log(member.getFullName());
351353

352354
You can't add properties to a constructor like you can with regular objects. If you want to add a feature to all objects at once, you have to use the prototype instead. So in this case,
353355

354-
`Person.prototype.getFullName = () => this.firstName + this.lastName`
356+
```js
357+
Person.prototype.getFullName = function () {
358+
return `${this.firstName} ${this.lastName}`;
359+
}
360+
```
355361

356362
would have made `member.getFullName()` work. Why is this beneficial? Say that we added this method to the constructor itself. Maybe not every `Person` instance needed this method. This would waste a lot of memory space, since they would still have that property, which takes of memory space for each instance. Instead, if we only add it to the prototype, we just have it at one spot in memory, yet they all have access to it!
357363

0 commit comments

Comments
 (0)