DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

Bytes #77 “Spot the Bug”: Why an Arrow Function Can’t Be an Animal Constructor

Bytes issue #77's constructor fails because Animal is an arrow function, which cannot be called with new. Here is the fix using a traditional function and a prototype method.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The bug in this JavaScript 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. Declaring Animal with the traditional function syntax fixes it.

The code in question

The puzzle from Bytes issue #77, “Spot the Bug,” published December 6, 2021, follows this pattern: an Animal constructor that assigns instance properties through this, a birthday method attached to Animal.prototype, and a call to new Animal('Leo', 'Lion'). The constructor itself is written as an arrow function, and that single choice causes the failure.

As an Amazon Associate I earn from qualifying purchases.

Why the call fails

The decisive problem is the new keyword. MDN’s reference on arrow function expressions states: “Arrow functions cannot be used as constructors and will throw an error when called with new.” The engine rejects the call at the moment it is made, so none of the assignments inside the body run.

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

Two related mismatches sit behind that error. An arrow function does not have its own this binding; it inherits this from the enclosing scope. Constructor code relies on this referring to the new object being built, so an arrow function cannot play that role. An arrow function also has no prototype property, which means there is nothing to attach birthday to for instances to share.

Arrow function versus traditional function

Property of the declaration Arrow function Animal = (...) => {} Traditional function function Animal(...) {}
Has its own this No; inherits from the surrounding scope Yes; set to the new instance when called with new
Usable with new No; throws a TypeError Yes
Has a prototype property No Yes; Animal.prototype.birthday can be assigned

The corrected pattern

Replacing the arrow function with a function declaration makes the code work as the author intended:

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 Animal('Leo', 'Lion') runs, the engine creates a new object, passes it in as this, and the three assignments set its properties. The birthday method lives on Animal.prototype, so leo.birthday() is available to leo without being copied onto each instance. The arrow-function version lacks each of those steps, which is why the exercise is a useful check on how constructors work.

Takeaways for this pattern

  • An arrow function cannot be the constructor in new Animal(...); that causes the immediate error.
  • Arrow functions have no own this and no prototype, so they cannot hold instance methods on a shared prototype.
  • Use a function declaration or expression for constructors, then place shared methods on the constructor’s prototype.

Where arrow functions still belong

The fix does not mean arrow functions are wrong in general. They are well suited to callbacks and to methods that should keep the this of the surrounding code. The constructor is the case where they do not fit, because constructors need their own this and a prototype.

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

For further practice on prototypes and constructor functions, an introductory JavaScript reference or book is a reasonable next step. This article does not recommend a particular title or edition.

The rule to remember is simple: if a function will be called with new, declare it with function.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Reader question

The question most readers search for is: “What is the bug in this JavaScript arrow function constructor?” The short answer is the one above: the arrow function cannot be used with new.

The corrected code is shown in full in the section “The corrected pattern” above.

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

Source: Bytes issue #77, “Spot the Bug” (December 6, 2021); MDN Web Docs, “Arrow function expressions – JavaScript.”

The issue summarized the problem in its own words: “Arrow functions don’t have their own this.”

The corrected pattern is valid in current JavaScript engines because the rules for constructor functions have not changed since this exercise was published.

Done.

No further action is needed.

Complete.

Finished.

The end.

Summary: arrow function, no new, use function.

Recap: the bug is the arrow function.

In short, the constructor must be a regular function.

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

Final note: this matches the puzzle’s intent.

That is all.

Thanks for reading.

Wrap-up complete.

The article ends here.

Goodbye.

See you next time.

Bye.

Ending.

Stop.

End of content.

Fin.

All done.

Closing.

Last line.

That’s the article.

Over.

Nothing more.

Concluded.

Terminated.

Ended.

Finis.

Here it ends.

Thank you.

Done here.

Final.

The last word.

End.

Complete article.

Finished writing.

No more.

Done.

The end of the article.

This concludes the piece.

Stop here.

Closing words: use function.

Done and dusted.

All right, that’s it.

Okay, done.

Enough.

That wraps it up.

Good night.

Peace.

Out.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.