Last active
June 26, 2020 01:46
-
-
Save geraldyeo/8bc96de98150c89c7fc9c2a774e83df8 to your computer and use it in GitHub Desktop.
Spot the bugs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Animal = (name, type) => { | |
this.name = name | |
this.type = type | |
this.age = 0 | |
} | |
Animal.prototype.birthday = function () { | |
this.age++ | |
} | |
const leo = new Animal('Leo', 'Lion') | |
/* | |
Arrow Functions don't have their own `this`. This leads to three errors in our code. | |
First, we're adding properties to `this` in the constructor function. Again, because | |
Arrow Functions don't have their own `this`, you can't do that. | |
Second, we can't use the `new` keyword with an Arrow Function. This will throw a `X is | |
not a constructor` error. | |
Third, we can't add a property on a function's prototype if that function is an Arrow | |
Function, again, because there's no `this`. | |
*/ | |
// Solution | |
function Animal (name, type) { | |
this.name = name | |
this.type = type | |
this.age = 0 | |
} | |
Animal.prototype.birthday = function () { | |
this.age++ | |
} | |
const leo = new Animal('Leo', 'Lion') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment