Chapter 11. Donât Modify Objects You Donât Own
One unique aspect of JavaScript is that nothing is sacred. By default, you can modify any object you can get your hands on. It doesnât matter if the object is developer-defined or part of the default execution environmentâitâs possible to change that object as long as you have access to it. This isnât a problem in a one-developer project, in which exactly what is being modified is always known by the one person whoâs in control of all code. On a multiple-developer project, however, the indiscriminate modification of objects is a big problem.
What Do You Own?
You own an object when your code creates the object. The code that
creates the object may not have necessarily been written by you, but as
long as itâs the code youâre responsible for maintaining, then you own
that object. For instance, the YUI team owns the YUI
object, and the Dojo team owns the dojo
object. Even though the original person who
wrote the code defining the object may not work on it anymore, the
respective teams are still the owners of those objects.
When you use a JavaScript library in a project, you donât
automatically become the owner of its objects. In a multiple-developer
project, everyone is assuming that the library objects work as they are
documented. If youâre using YUI and make modifications to the YUI
object, then youâre setting up a trap for
your team. Someone is going to fall in, and itâs going to cause a
problem.
Remember, if ...
Get Maintainable JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.