Set#toString and Map#toString
# Allen Wirfs-Brock (11 years ago)
ES6 defines Map.prototype[Symbol.toStringTag]
and Set.prototype[Symbol.toStringTag]
such that the ES6 version of Object.prototype.toString will produce
"[object Map]" and "[object Set] respectively when applied to Map or Set instances
# Boris Zbarsky (11 years ago)
On 9/4/14, 5:51 PM, C. Scott Ananian wrote:
s.toString() '[object Object]'
Per spec this should be "[object Set]". Both V8 and SpiderMonkey get this right, fwiw.
m.toString() '[object Object]'
Again, should be "[object Map]".
Eventually node's
util.inspect
(and the various browser analogs) will presumably learn aboutMap
andSet
. But the latest Chrome beta still reports:
> s = new Set(['a']) < Set {}
Firefox reports:
Set [ "a" ]
Neither
Set
norMap
havetoString
methods defined on the prototype. This can lead to somewhat surprising behavior for interactive users (this is node withes6-shim
):> s = new Set(['a']) {} > m = new Map([['a','b']]) {} > s.toString() '[object Object]' > m.toString() '[object Object]'
Eventually node's
util.inspect
(and the various browser analogs) will presumably learn aboutMap
andSet
. But the latest Chrome beta still reports:> s = new Set(['a']) < Set {} > s.has('a') < true
Since the potential for confusion with an empty object is clear, might it make sense to define
Set#toString
andMap#toString
methods in ES6? I'm thinking that:Set.prototype.toString = function() { return Array.from(this).toString(); }
Not sure about
Map#toString
, but the equivalent definition:Map.prototype.toString = function() { return Array.from(this).toString(); }
gives somewhat confusing results. Eg:
> (new Map([['k1','v1'],['k2','v2']])).toString() 'k1,v1,k2,v2'
What do y'all think?