@@toStringTag spoofing for null and undefined
Domenic Denicola
d at domenic.me
Tue Jan 20 11:45:56 PST 2015
From: es-discuss [mailto:es-discuss-bounces at mozilla.org] On Behalf Of Jordan Harband
> It appears that TC39 considers it important to not break existing JS code with spec changes. Thus, `Object.prototype.toString.call(foo)`, for any ES5 value "foo", must always and forever return the same value that it returned in ES5 - otherwise, existing code may follow different code paths in ES5 versus ES6, which is a hazard. This leads me to the belief that @@toStringTag values on ES5 builtins should never be changeable.
I don't think this reasoning, or in fact the reasoning that leads to the current tilde-prefixing, is sound.
First we must realize that if we were to remove the tilde-prefixing, no deployed code would ever break. Only new code (which returns e.g. "Array" from the @@toStringTag) *interacting with old code* (which tests for "[object Array]") would be affected.
But in these cases developers have plenty of opportunities to test the interaction. And they quite probably are introducing the "Array" return value specifically to go down a specific code path in the old code. (For example, if they have created an array-like proxy that behaves like an Array in all other ways.) The current spec forces them to instead override Object.prototype.toString itself if they want to go down that path.
So I think the tilde-prefixing is certainly not necessary, and in fact is counterproductive in all cases I can think of.
More information about the es-discuss
mailing list