Is using Prototype to extend native objects bad? [closed]

I read somewhere that using prototype to extend native objects (String, Number, Array, etc.) was a bad idea. Is this true? Why or why not?

Answers:

Answer

I don't think it's bad. If you have a look at Ruby on Rails, very many native objects are extended with custom functionality and it's a very good framework.

What would be bad is if you change existing methods on native objects. this could cause unforseen consequences.

Answer

There's a great discussion about this in this video from JSConf 2011 by Andrew Dupont. http://blip.tv/jsconf/jsconf2011-andrew-dupont-everything-is-permitted-extending-built-ins-5211542

Basically the points are:

  • Don't extend Object.prototype
  • Some people might like to extend things, some people don't
  • You need to know what you're doing
  • Don't use two libraries that extend things, because it can be bad
  • Extending prototypes to add standard functionality is almost always ok.
Answer

I would stay clear of extending/modifying behavior of native objects.

It at least makes sense when developing in a team environment.

Simply because, months later, another developer writing another independent piece of code isn't immediately going to recognize the changed behavior unless documented somewhere and made aware of it prior to starting his task.

Instead, I suggest encapsulating/"namespace"-ing all such functionality such that somebody may chose to or not to use the modified functions.

Also, native objects and their methods are thoroughly tested for wide-ranging cases. So you you'd have to be completely sure of what you're doing before modifying native functionality.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.