Object.prototype JavaScript Code Extension

I do not ask if everything is in order:

Object.prototype.method = function(){}; 

This is considered evil by almost everyone, considering it a mess of for(var i in obj) .

Real question

Ignoring

  • Incompetent browsers (browsers that do not support Object.defineProperty )
  • Potential for collision of properties or overriding

Assuming you have a useful method unbelievably , is this considered wrong / unethical?

 Object.defineProperty(Object.prototype, 'methodOnSteriods',{ value: function(){ /* Makes breakfast, solves world peace, takes out trash */ }, writable: true, configurable: true, enumerable: false }); 

If you think the foregoing is unethical, why would they even implement this feature in the first place?

+32
javascript ecmascript-5
Jul 29 '11 at 17:51
source share
5 answers

I think this is normal if it works in your target environment.

I also think that the paranoia of prototype expansion is bloated. As long as you use hasOwnProperty() as a good developer, everything is fine. In the worst case, you overload this property elsewhere and lose this method. But this is your own mistake if you do.

+23
Jul 29 '11 at 18:00
source share

I would say that it is almost as evil as before. The biggest problem, still the same as before, is that Object.prototype is global. Although your method may currently solve world peace, it may rewrite some other method (which guarantees a galactic world) or it may be rewritten in the future by some library that you do not control (therefore, immerse the world in chaos)




Newer versions of Javascript have many features related to properties, such as defining a property that can be enumerable / not enumerable using getters and setters ... Object.defineProperty exists to control this.

From Mozilla Docs :

This method allows you to precisely add or change a property on an object. The usual addition of a property through an assignment creates properties that appear when listing properties (for ... in a loop), whose values ​​can be changed and which can be deleted. This method allows you to change these additional details by default.




This new feature is mainly needed to support new features, and you should use it yourself. The ability to modify Object.prototype is only a side effect of it, which is also a “normal” object and just as evil as before.

+8
Jul 29 '11 at 18:00
source share

Good in “JavaScript: the good parts”, there is a similar function, I think it is very useful to improve the basic javascript objects (like String, Date, etc.), but only for that.

 // Add a method conditionally. from "JavaScript: the good parts" Function.prototype.method = function (name, func) { if (!this.prototype[name]) { this.prototype[name] = func; } } 
+3
Jul 29 '11 at 18:29
source share

.hasOwnProperty() eliminates iteration through inherited properties that I personally find are often more annoying than useful. This pretty much defeats the usefulness of Object.create() , which is ironic, since the same guy who convinced everyone to do .hasOwnProperty() also contributed to Object.create() .

Object.prototype should not be extended for the reasons listed here. If you really want to expand it, make the extensions inoperable.

I understand that this flies in front of all published best practices, but we really need to stop "mandating" .hasOwnProperty() on iteration of the keywords of the objects and use the utility of direct inheritance of the object-object.

+3
Sep 15 '16 at 21:48
source share

Short answer: Yes, you have to do this.

Before you do this, you need to take a few precautions:
1. using hasOwnProperty when iterating an object, but this is not a precaution when iterating an object, I already use hasOwnProperty anyway.
2. check if name exists in Object.prototype.name , it is very safe to avoid name collision.
3. use Object.defineProperty() , just add an extra protective layer.

As you can see, this is not very difficult.

Now the benefits come when you take care of the risks / disadvantages:
1. The chain of the method, it just makes the code more readable, concise and makes the coding more enjoyable. In turn, makes you happier and your life easier.
2. solves the problem of compatibility with the browser, you still make a polyfill.

PS:
Do not do this when you are working with a large team.

+1
Aug 31 '16 at 4:56
source share



All Articles