Why is extending built-in javascript objects not a good idea?

Extending built-in Object/types or Array is a not a good idea in Javascript. Why ?

  idkblogs.com      August 1, 2021

Shubham Verma

Shubham Verma


Why is extending built-in javascript objects not a good idea?

Why is extending built-in javascript objects not a good idea?

Extending built-in Object/types or Array is a not a good idea in Javascript. Because other modules can be affected, this is especially true for the Object prototype in javascript, as everything in Javascript extends from objects.

Let's have a look about What is extending an object in JavaScript?
When you add any functionality to an object using prototype. Example,

In the above code we are extending the functionality, added a method 'info'.

Now, let's talk about Why is extending built-in javascript objects not a good idea.

Extending the JavaScript built-in object is not a good idea because if browser/JS has decided that they will provide the same method that you have extended, then your method will be override and the JS implementation (which may be difference from yours) would take over.

So not extending in the first place is future proofing your codes, on the other side, If you decide to overwrite the browsers definition, any future developer working on your code won't be able to know above chnages. If you had to do the same then you have to make a library in your project and put these extending logic over there. So that you can easily marked those extensions and there won't be any conflicts.

In general, extending built-in javascript objects is not a bad idea, because you do not control what will happen in the future to built-in classes and you can not stop yourself to scale your application. When you extend the built-in objectsresponsibly, and with understanding of its consequences, prototypal inheritance is an excellent way of keeping is good to have.


Thank you

for taking the time to read this article. If you’re interested in Node.js or JavaScript this link will help you a lot.

If you found this article is helpful, then please share this article's link to your friends to whom this is required, you can share this to your technical social media groups also. You can follow us on our social media page for more updates and latest article updates.
To read more about the technologies, Please subscribe us, You'll get the monthly newsletter having all the published article of the last month.