Skip to content

Why you should not modify a JavaScript object prototype

New Course Coming Soon:

Get Really Good at Git

Find out why you should not modify a JavaScript object prototype and what to do instead

As programmers, one of the skills we must first learn is how to search for a solution.

Google is your friend. And most of the times a StackOverflow answer back from 2009 is the perfect solution to your 2019+ problem.

On that specific site, or on personal blogs, I sometimes see code that modifies built-in Objects prototypes.

Like in this example, where the Array object prototype is enhanced by adding an insert method:

Array.prototype.insert = function(index, item) {
  this.splice(index, 0, item)
}

In this way, you can have any array and call insert() on it:

['red', 'blue'].insert(0, 'yellow')

It’s handy. Instead of having to define such function and worry about the scope of it, you attach it to the Array object, so that every array has it available.

But just because you can, it does not mean you should.

What’s wrong with this approach?

Possible conflicts

Suppose a library you use implements such thing. And another library you import does the same. Perhaps the methods work slightly differently, and things seem to work fine until they don’t.

You have a big problem here, because you can’t modify those libraries but you still want to use them.

Future-proofing your code

Suppose the next version of JavaScript implements the Array.insert method. With a different signature. Now what happens? You need to go back and rewrite all the code you wrote. Perhaps for a client you don’t work for anymore.

Or maybe you did this in a library that’s used by other people in their own projects. That would be even worse.

This approach only creates technical debt and pretty much invites problems.

What should you do instead?

Create a function in a library file and import it when you want to use it. Don’t modify objects you have no control over.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching May 21, 2024. Join the waiting list!
→ Get my JavaScript Beginner's Handbook
→ Read my JavaScript Tutorials on The Valley of Code
→ Read my TypeScript Tutorial on The Valley of Code

Here is how can I help you: