DEV Community

chandra penugonda
chandra penugonda

Posted on

1 1

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)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay