Skip to content

Object literals and Array literals don't share the prototype of the Object and Array built-in classes respectively #34803

Closed
@ghost

Description

What steps will reproduce the bug?

"use strict";

console.log(
	"%s\n%s\n%s\n%s\n%s\n%s",

	typeof [] === "object", // supposed to be true

	[] instanceof Object, // supposed to be true

	[] instanceof Array, // supposed to be true

	Array.isArray([]), // supposed to be true

	Symbol.iterator in [], // supposed to be a true

	Object.getPrototypeOf([]) === Array.prototype // supposed to be true
);

How often does it reproduce? Is there a required condition?

100% reproduction rate, it always has the same results.

What is the expected behavior?

It is expected that object literals share their prototype with Object, and that array literals share their prototype with Array, thus an instanceof check against Object or Array respectively should show true, and this is true for all browsers, Deno, is documented at MDN, yet I couldn't find what the ECMAScript spec had to say about this.

Interestingly, the Node terminal repl showed the expected results, while running a file didn't.
This was run in strict mode and in sloppy mode, without a difference in output.

What do you see instead?

Array literals do not share the prototype of the global Array class.

Additional information

This may not be standard compliant, and shouldn't break any legacy code, as it would be odd to check that an array is an array through an instanceof check instead of Array.isArray.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions