My rules of thumb for extending JavaScript native objects
I have the following rules of thumb for extending JavaScript’s native objects:
- Don’t extend things if you’re making a library.
- Shims are okay in the browser.
- Don’t touch
Object.prototype
.
Let’s explore these in a little more depth.
Don’t extend things if you’re making a library. Libraries should be self-contained within a namespace (unless you’re explicitly extending the native objects; Sugar and Prototype are good examples).
Shims are okay in the browser. You usually want to use native methods, but old browsers might not have support for newer JavaScript features. If you define
Array.forEach
if it’s not defined, that’s okay. If you’re not in a browser environment, why aren’t you on the latest version of JavaScript?Don’t touch
Object.prototype
. You can break enumeration as detailed here.
Those are my rules!