Wednesday, 8 March 2017

Why does the util module modify __proto__ in inherits(), when MDN strictly advises against doing so

In util.inherits, the prototype of derived class is modified here, so that its [[Prototype]] becomes base class's .prototype.Whereas MDN warns us against it.Changing the [[Prototype]] of an object is, by the nature of how modern JavaScript engines optimize property accesses, a very slow operation, in every browser and JavaScript engine. The effects on performance of altering inheritance are subtle and far-flung, and are not limited to simply the time spent in obj.__proto__ = ... statement, but may extend to any code that has access to any object whose [[Prototype]] has been altered. If you care about performance you should avoid setting the [[Prototype]] of an object. Instead, create a new object with the desired [[Prototype]] using Object.create().While Object.prototype.__proto__ is supported today in most browsers, its existence and exact behavior has only been standardized in the ECMAScript 2015 specification as a legacy feature to ensure compatibility for web browsers. For better support, it is recommended that only Object.getPrototypeOf() be used instead.If changing the __proto__ or [[Prototype]] is such a bad idea, why does node do it?

Submitted March 09, 2017 at 12:17AM by zhirzh

No comments:

Post a Comment