Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

The bug in Bytes issue #77’s “Spot the Bug” exercise is that Animal is declared as an arrow function and then called with new. Arrow functions cannot be constructors, so new Animal('Leo', 'Lion') throws a TypeError before any instance is created. The fix shown in the issue is to declare Animal with the traditional function syntax.

Why the call to new fails

The exercise combines three pieces: a constructor that assigns properties through this, a method added to Animal.prototype, and an instance created with new. The first two are a standard pattern for building objects in JavaScript. The problem is that the constructor is an arrow function, and the MDN Web Docs reference for arrow function expressions is direct about it: “Arrow functions cannot be used as constructors and will throw an error when called with new.”

The three mismatches in the pattern

The failing new call is the immediate symptom, but the arrow function conflicts with the pattern in three separate ways:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • No constructor behavior. An arrow function cannot be the target of new, so the runtime refuses to create an instance.
  • No own this. An arrow function takes this from the scope where it is defined. The issue states the point plainly: “Arrow functions don’t have their own this.” Inside Animal, this.name = name would not write to a new object.
  • No prototype property. Shared methods are attached to Animal.prototype. Arrow functions do not have that property, so the birthday method has nowhere to live as a shared method for instances.

Any one of these would break the design. Together they mean the arrow function cannot play the role of a constructor at all.

The corrected version

Replacing the arrow function with a function declaration fixes all three points. The corrected code from the issue is:

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');

When new runs, JavaScript creates a new object, binds it as this inside Animal, and links it to Animal.prototype. Each instance then inherits birthday from the prototype instead of storing its own copy.

Arrow function versus constructor function

Property of the function Arrow function (const Animal = (...) => {}) Traditional function (function Animal() {})
Can be called with new No; throws a TypeError Yes
Own this binding No; inherits this from the enclosing scope Yes; set to the new instance when called with new
Has a prototype property No Yes
Suitable for Animal.prototype.birthday sharing No Yes

Checking the fix

  1. Open a browser console or run Node.js, and paste the corrected Animal code.
  2. Run const leo = new Animal('Leo', 'Lion');. It should return an object with name, type, and age set to 0.
  3. Run leo.birthday(); leo.age;. The result should be 1.
  4. To see the original failure, declare const Animal = (name, type) => { ... }; with the same body and run new Animal('Leo', 'Lion'). The engine throws a TypeError stating that Animal is not a constructor.

About the issue

Bytes published “Spot the Bug” as issue #77 on December 6, 2021. Its answer section lists the same problems covered above: the arrow function’s missing own this, its inability to be used with new, and the missing prototype support. The language rules involved have not changed, so the explanation and the fix remain valid for current JavaScript engines.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Practical rule

Use a traditional function declaration or expression whenever a function is meant to be a constructor and share methods through its prototype. Arrow functions remain the better choice for callbacks and short functions that should keep the surrounding this, but they are the wrong tool for this kind of object construction. Modern class syntax, class Animal { ... }, is another way to write the same constructor and prototype pattern, and it also cannot be invoked without new.

“

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.