5

Recently, given the changes to defining properties in ECMAScript 5, I have revisited the question of whether we can safely extend the native JavaScript prototypes. In truth, all along I have extended prototypes like Array and Function, but I avoided doing so with Object, for the obvious reasons. In unit testing with Jasmine, by adding Object.prototype specs to specs for my own personal framework, extending Object.prototype with non-enumerable functions has appeared to be safe. Data properties like a "type" property, however, with getters/setters that do any unusual processing have had unintended consequences. There is still the possibility of conflicts with other libraries--though in my work, that hardly ever comes up. Nevertheless, as long as the functions are not enumerable, it looks like extending Object.prototype can be safe.

What do you think? Is it safe to extend Object.prototype now? Please discuss.

Constantine
  • 1,015
  • 1
  • 8
  • 12
  • I would *broadly* agree that extending object with non-enumerable descriptors is safe. As for conflicts, these can be minimised if properly namespaced, i.e. you might add `String.prototype.myExtensions.someMethod` rather than just `String.prototype.someMethod`. – Mitya Aug 02 '12 at 16:34
  • Extending an object with another object, kind of like a merge function, would be very useful. – starbeamrainbowlabs Aug 02 '12 at 16:39
  • 1
    Years have passed, now there's a question about [Modifying built-in prototypes in ES6](http://stackoverflow.com/q/36746555/1048572) :-) – Bergi Jun 04 '16 at 16:41

2 Answers2

1

Extending objects native to JavaScript might become a little safer, through many collision concerns still stand. Generally, unless you're extending object to support standartized behavior from more recent standard it really would be still much safer to introduce wrapper - it is much easier to do things right way when you're the only one in control.

Speaking of objects native to environment (DOM elements and nodes, AJAX stuff), new JS standard still don't give and, arguably, can't give you any guarantee about any interaction with those except what defined in their interface standard. Never forget that they're potentially accessible through many different scripting engines and thus not need to be tailored for quirks of one specific language - JS. So recommendation to not extend those either still stands as well.

Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68
  • I was going to wait a while before selecting a "winner," but I think the wrapper, or "decorator," is still the safest thing to do. Collisions especially are a worry. Besides, one can ask, why take the risk? – Constantine Aug 02 '12 at 22:54
0

The definitive, absolute answer is ...

"It depends." :)

Extending any built in JavaScript object can be perfectly safe or it can be a complete disaster. It depends on what you are doing and how you are doing it.

Use smart practices and common sense and test the hell-out-of-it.

grahamesd
  • 4,773
  • 1
  • 27
  • 27