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 ?

🙋‍♂️ Shubham Verma    🗓 August 1, 2021


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. In this post we will learn why is extending built-in javascript objects not a good idea?

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.

Conclusion:

After reading this article, it is cleared that Why extending built-in javascript objects are not a good idea. In this article, we learned What is extending an object in JavaScript? and how we can add functionality to an object using prototype.


Support our IDKBlogs team

Creating quality content takes time and resources, and we are committed to providing value to our readers. If you find my articles helpful or informative, please consider supporting us financially.

Any amount (10, 20, 50, 100, ....), no matter how small, will help us continue to produce high-quality content.

Thank you for your support!




Thank you

I appreciate you taking the time to read this article. The more that you read, the more things you will know. The more that you learn, the more places you'll go. 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.