Set#toString and Map#toString

Allen Wirfs-Brock allen at wirfs-brock.com
Thu Sep 4 16:34:01 PDT 2014


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

Allen


On Sep 4, 2014, at 2:51 PM, C. Scott Ananian wrote:

> Neither `Set` nor `Map` have `toString` methods defined on the
> prototype.  This can lead to somewhat surprising behavior for
> interactive users (this is node with`es6-shim`):
> 
> ```js
>> 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 about `Map` and `Set`.  But the latest Chrome
> beta still reports:
> ```js
>> 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` and `Map#toString` methods in
> ES6?  I'm thinking that:
> ```js
> Set.prototype.toString = function() { return Array.from(this).toString(); }
> ```
> Not sure about `Map#toString`, but the equivalent definition:
> ```js
> Map.prototype.toString = function() { return Array.from(this).toString(); }
> ```
> gives somewhat confusing results.  Eg:
> ```js
>> (new Map([['k1','v1'],['k2','v2']])).toString()
> 'k1,v1,k2,v2'
> ```
> 
> What do y'all think?
> --scott
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> 



More information about the es-discuss mailing list