Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ghost opened this issue Aug 16, 2020 · 2 comments

Comments

@ghost
Copy link

ghost commented Aug 16, 2020

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.

@addaleax
Copy link
Member

$ cat x.js 
"use strict";

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

	typeof [], // 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
);
$ node -v
v12.18.2
$ node x.js 
object
true
true
true
true
true

Anything else about your environment that may be relevant here? Are you setting NODE_OPTIONS in any way, or setting any other relevant environment variables/command line flags?

@ghost
Copy link
Author

ghost commented Aug 17, 2020

$ cat x.js 
"use strict";

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

	typeof [], // 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
);
$ node -v
v12.18.2
$ node x.js 
object
true
true
true
true
true

Anything else about your environment that may be relevant here? Are you setting NODE_OPTIONS in any way, or setting any other relevant environment variables/command line flags?

Interesting, it turns out that my environment was setup incorrectly, I'll close this and report it to those who setup the environment instead.

@ghost ghost closed this as completed Aug 17, 2020
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant