DEV Community

chandra penugonda
chandra penugonda

Posted on

1. Spot the Bug

Arrow function as constructor

const Animal = (name, age) => {
  this.name = name;
  this.age = age;
};

Animal.prototype.birthday = function () {
  this.age++;
};

const animal = new Animal('Leo', 'Lion');

Enter fullscreen mode Exit fullscreen mode

Bug Details

  • Arrow functions don't have their own this keyword. So, adding properties will leads to Error.
  • we can't add property on function prototype if it's arrow function. Again no this keyword.
  • we cannot use new keyword on arrow function. This will throw error X is not a constructor

Top comments (0)