Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the benefits and dangers of adding methods to Object.prototype in Javascript?

I know this was a contentious issue five years ago, but I'm wondering if things have changed for today's JavaScript. Are there any real world examples of a major modern library being incompatible with extending Object.prototype?

I'm not interested in hypothetical "someone may write bad for in iteration code in a library that you want to use, maybe, in the future, and then you might get a weird bug"

like image 675
Daniel X Moore Avatar asked Sep 30 '10 16:09

Daniel X Moore


People also ask

What is the benefit of prototype in JavaScript?

Prototypes allow you to easily define methods to all instances of a particular object. The beauty is that the method is applied to the prototype, so it is only stored in the memory once, but every instance of the object has access to it.

What are prototype methods in JavaScript?

Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what's called a prototype chain. The chain ends when we reach a prototype that has null for its own prototype.

What is the difference between adding a method inside the constructor and adding it to the prototype?

One big difference is that methods/properties added via prototype are only stored once (objects “containing” the prototype just “reference” the method/property) whereas methods/properties added in the constructor are parsed and copied for each instance created by that constructor.

Why are prototypes more efficient than other techniques for creating classes in JavaScript?

There is a clear reason why you should use prototypes when creating classes in JavaScript. They use less memory. When a method is defined using this. methodName a new copy is created every time a new object is instantiated.


1 Answers

Are there any real world examples of a major modern library being incompatible with extending Object.prototype?

Yes, I remember problems with jQuery, -which is one of the less intrusive libraries- for example:

  • I tried to prototype a length() method to Object and broke jQuery – how?

Another case that I remember is that someone added a load function to the Object.prototype object, and it caused problems with the $().load event:

// DON'T DO THIS ! :)
Object.prototype.load = function () {};

​$(window).load(function () {
  alert('load event'); // never fired
});​

Example here.

Augmenting the Object.prototype object in that way is never recommended, because those properties will be inherited by a great number of objects -even also by some host objects-, and as you know, the primary concern is that they will be enumerated by the for-in statement.

In ECMAScript 5, now a safer way exist, because we can now declare non-enumerable properties, for example:

Object.defineProperty(Object.prototype, 'foo', { value: 'bar' });

In the property descriptor -{ value: 'bar' }- we can specify property attributes, in the case of Value Properties as in the above example, we can specify the writable attribute, and the common configurable attribute (determines if a property can be re-configured -attribute changes- or deleted.

And we have also the enumerable attribute, which determines if the property will be enumerated by the for-in statement.

If we don't specify the attributes, they are false by default, the descriptor will look like:

{
  value: 'bar',
  writable: false,
  configurable: false,
  enumerable: false
}
like image 63
Christian C. Salvadó Avatar answered Nov 24 '22 00:11

Christian C. Salvadó