Skip to content

Instantly share code, notes, and snippets.

@geraldyeo
Last active June 26, 2020 01:46
Show Gist options
  • Save geraldyeo/8bc96de98150c89c7fc9c2a774e83df8 to your computer and use it in GitHub Desktop.
Save geraldyeo/8bc96de98150c89c7fc9c2a774e83df8 to your computer and use it in GitHub Desktop.
Spot the bugs
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