DEV Community

loading...

1. Spot the Bug

chandramarch18
Senior Engineer @ Loco
・1 min read

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

Discussion (0)