Peter Olson
Peter Olson

Reputation: 142977

Is there something wrong with extending native Javascript prototypes?

I have seen some people regard extending the natives prototypes in Javascript with disdain. I was thinking about doing it, because of syntactic convenience. I.e.

function(array, element)

can be more cumbersome to write and less readable than

array.function(element)

But the second can only be acheived (AFAIK) by extending the Array prototype. Is there something wrong with extending native prototypes, and will doing this somehow haunt be me later?

Upvotes: 1

Views: 188

Answers (1)

jfriend00
jfriend00

Reputation: 707786

It can conflict with other libraries trying to do the same.

It can conflict with future methods added to native objects.

If anyone is using for (var i in array) without proper hasOwnProperty() checks, their code may/probably will break because the new method may show up in the iteration in older browsers.

Upvotes: 1

Related Questions