Making Object Literals a sub-class of Object

Rick Waldron waldron.rick at gmail.com
Fri Oct 14 14:33:20 UTC 2016


On Fri, Oct 14, 2016 at 9:05 AM Brian Ninni <ninni.brian at gmail.com> wrote:

> I did a quick search and didn't find any recent mentions of this topic.
>
> On more than one occasion I've had to determine whether something was a
> plain old Object, or some other class. This involves checking that the
> given object was NOT an instanceof any other acceptable class.
>
> Array, RegExp, Function, and Class Literals all already create an Object
> sub-class, so why not Object Literals?
>

Terminology clarification: "Object literal" refers to the syntactic grammar
definition.



> It doesn't have to operate any differently than a standard Object does
> (though it allows room for deviation in the future), just have a different
> constructor so it can easily be determined whether it is a literal or not.
>
> This would break code that uses `obj.constructor === Object`, but that
> code is not always reliable since the 'constructor' property can be
> overwritten without any side-effects anyway.
>
> Are there any other major reasons why this is a bad idea?
>


Objects created by `new Object()`, `Object()`, `let/const/var o = {}` are
functionally equivalent*—changing the runtime semantics of only the
syntactic form would be a web-breaking change.

* Follow:
- https://tc39.github.io/ecma262/#sec-object-value
- https://tc39.github.io/ecma262/#sec-ordinarycreatefromconstructor
-
https://tc39.github.io/ecma262/#sec-object-initializer-runtime-semantics-evaluation


Rick

>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20161014/26c3f56d/attachment-0001.html>


More information about the es-discuss mailing list