Array methods applied to strings

David-Sarah Hopwood david-sarah at jacaranda.org
Mon May 11 13:05:50 PDT 2009


James Graham wrote:
> It seems that ES5 throws a TypeError for constructs like
> 
> Array.prototype.pop.call("abc")
> 
> whereas current implementations (and, I think ES3, but I didn't check
> that closely) will return "c".

Array.prototype.pop is generic, and it will work on mutable array-like
objects without throwing a TypeError. However, "abc" is not a mutable
array-like object: its 'length' property and its array index properties
have attributes [[Writable]]:false and [[Configurable]]:false.
So in ES5, 'pop' applied to a string should throw a TypeError when it
fails to delete the property at index 2. This is intentional, AFAICS.

In ES3, strings were not specified to have array index properties
(although this was not forbidden), and so 'pop' was not guaranteed to
work on them for that reason.


(I don't think it matters to the above question, but: is "abc" coerced
to a String wrapper here? It depends on whether 'Array.prototype.pop'
is treated as a strict or non-strict function, but I can't see where
that is specified for "native" functions.)

-- 
David-Sarah Hopwood ⚥



More information about the es5-discuss mailing list