Object.seal, read references, and code reliability
alexkodat at gmail.com
Tue Aug 15 15:10:58 UTC 2017
This is interesting but don’t think it affects the arguments for/against Object.guard significantly. For objects that don’t have Object.prototype in their prototype chain, don’t guard them. Or guard them if it suits your needs. And add or don’t add valueOf, toString, etc.. to the objects or their prototypes, depending on your needs. You would certainly not use Object.guard on every object.
My pictured common use case is a constructor or block of code would create an object that represents a bunch of related data and, when done, Object.guards and Object.seals the object. We already tend to do the latter. This would catch typos (yes, only when they’re executed) and also forces unruly programmers (including me) to add properties in one place so it’s easy to see what all the properties are on an object (Object.seal accomplishes this).
For APIs we tend to almost always use closures as, of course, these provide much better encapsulation than objects. We’ve never bothered to Object.seal/Object.freeze such API objects as assigning to a property in them is simply is not something someone does accidentally. But, now that I think about it, we might Object.guard them as it would catch the occasional boo-boo where one forgets to put the () on a function call (I do this every now and then). Not a huge deal but perhaps a little useful.
From: T.J. Crowder [mailto:tj.crowder at farsightsoftware.com]
Sent: Tuesday, August 15, 2017 8:50 AM
To: Alex Kodat <akodat at rocketsoftware.com>
Cc: Jordan Harband <ljharb at gmail.com>; es-discuss at mozilla.org
Subject: Re: Object.seal, read references, and code reliability
On Tue, Aug 15, 2017 at 2:43 PM, Alex Kodat <akodat at rocketsoftware.com <mailto:akodat at rocketsoftware.com> > wrote:
> Sure but these all exist on Object.prototype so these would never
> throw for guarded objects. Maybe I'm missing something here?
Not all objects inherit from `Object.prototype`. Example:
const o = Object.create(null);
console.log("valueOf" in o); // false
In fact, not even all objects with prototypes inherit from `Object.prototype`:
const b = Object.create(null);
console.log("valueOf" in b); // false
console.log(Object.getPrototypeOf(b)); // null
const d = Object.create(b);
console.log("valueOf" in d); // false
console.log(Object.getPrototypeOf(d)); // b
-- T.J. Crowder
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the es-discuss