0

I've got that extending of the Object.prototype is full of the pitfalls. Is there any when extending other standard Javascript prototypes: String.prototype, Array.prototype, Function.prototype?

Thanks in advance.

[Edit] Related: What are pitfalls of extending Object.prototype?

Community
  • 1
  • 1
Dmitry Poroh
  • 3,705
  • 20
  • 34
  • This question is related - http://stackoverflow.com/questions/7141734/extending-core-types-without-modifying-prototype – maerics Aug 16 '12 at 14:41

2 Answers2

0
  1. If future browser versions implement Array.prototype.remove (either because of an upgrade to the EcmaScript standard, or through their own volition), their implementation will be overridden by our custom one, which will not only be less efficient (we can’t manipulate browser engine internals in the service of method optimization) but more importantly, they might have a different, non standard outcome.

  2. Extending natives messes with the object iteration cycle. The argument goes like this: since for in loops will visit all enumerable properties in the object’s prototype chain, custom native properties will unexpectedly be included in such iterations.

  3. Descendants of Object.prototype (i.e. every object whose prototype is not explicitly null) will lose access to the extended property if they happen to define a property with the same name.

http://javascriptweblog.wordpress.com/2011/12/05/extending-javascript-natives/

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
-1

Really helpful

Extending Natives in Javascript

[Edit] This should not only show you can but how to do it with avoiding pitfalls. :)

So is Extending Natives ever okay?

I’ve described some reasons for not augmenting native prototypes; you may know of others. You need to decide whether each of these concerns will be addressed by your planned extension, and whether the extension would add power and clarity to your codebase.

Zac
  • 2,201
  • 24
  • 48