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

Map and Set objects are not identified correctly #32

Closed
lll000111 opened this issue Dec 4, 2018 · 2 comments
Closed

Map and Set objects are not identified correctly #32

lll000111 opened this issue Dec 4, 2018 · 2 comments

Comments

@lll000111
Copy link

Extracted from #29 (comment)

Just FYI, there is a similar issue in React Native's runtime: facebook/react-native#19594

// Common stuff that works:
Object.prototype.toString.call(new Array())
// [Object Array]
Object.prototype.toString.call(new Date())
// [Object Date]

// Map and Set don't work:
Object.prototype.toString.call(new Set())
String(new Set())
// [Object object] but should be [Object Set]

Object.prototype.toString.call(new Map())
String(new Map())
// [Object object] but should be [Object Map]

This self-identification of constructors in JS is used quite frequently. If it is incorrect hacks have to be used.

@ThomasRogg
Copy link
Member

Will change to Symbol.toStringTag when we have time.
Till then, it works perfectly the way it is. Just keeping this issue open to not forget.

In comment to commit:

Is this the best option, instead of setting Symbol.toStringTag for each object type and leaving Object.prototype.toString unchanged?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/@@toStringTag

https://tc39.github.io/ecma262/#sec-object.prototype.tostring (item 15 "Let tag be ? Get(O, @@toStringTag).")

@ThomasRogg ThomasRogg reopened this Jan 16, 2019
@lll000111
Copy link
Author

Correction: I meant

Object.defineProperty(
    Map.prototype, 
    Symbol.toStringTag, 
    {
        writable:false,
        enumerable:false,
        configurable:true,
        value:'Map'
    }
);

http://2ality.com/2015/09/well-known-symbols-es6.html#overriding-the-default-tostring-tag

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

2 participants