Making Object Literals a sub-class of Object

Oriol Bugzilla oriol-bugzilla at hotmail.com
Fri Oct 14 14:24:17 UTC 2016


Object literals (object initializers) are just a convenient way to create a new ordinary object which (initially) inherits from `Object.prototype`, and populate it with some properties.

I don't think you should be able to distinguish them from similar objects not created using object initializers, e.g. `new Object()`.

As already said, you can use `Object.getPrototypeOf(obj) === Object.prototype` to check if `obj` inherits from `Object.prototype`.

But be aware that object literals can return object with a modified prototype:

```js
var obj = {__proto__: Array.prototype};
Object.getPrototypeOf(obj) === Object.prototype; // false, but was created using object literal!
```

And the condition may hold for objects not created using object literals, e.g.

```js
var obj = new Object();
Object.getPrototypeOf(obj) === Object.prototype; // true, but was not created using object literal!
```

Maybe you consider `new Object()` to be an object literal, so the above would be OK.

But there can also be non-ordinary objects whose prototype is `Object.prototype`:

```js
var obj = Object.setPrototypeOf([], Object.prototype);
Object.getPrototypeOf(obj) === Object.prototype; // true, but it's an array!
```

Maybe what would be more interesting is having a way to check if an object is ordinary.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20161014/67c9d778/attachment.html>


More information about the es-discuss mailing list