String(symbol)

Jeff Walden jwalden+es at mit.edu
Tue Aug 26 09:27:24 PDT 2014


On 08/26/2014 09:14 AM, Brendan Eich wrote:
> Claude Pache wrote:
>> Personally, I use the following expression in order to coerce a variable to a string:
>>
>>     var obj = this + ''
>>
>> and its in-place variant:
>>
>>     x += ''
>>
>> I think it'll continue to work in ES6.
> 
> Agreed, this is the correct-and-most-concise way to do it.

It's not the most correct way to do it.  |a + ""| performs ToPrimitive(a, hint = None), whereas ToString(a) performed ToPrimitive(a, hint = String).  The former consults valueOf, then toString, the latter the reverse.

> using String(any) to invoke ToString is verbose and fragile, because String could be rebound :-P.

Generally polyfills are fine with that limitation, and only us language pedants need worry/care about String being rebound.  All the array-method polyfills we've provided on MDN depend on non-rebinding of some builtin function or other.  It's not that big a deal.

Jeff


More information about the es-discuss mailing list