Implementing an identical JSON.stringify
Claude Pache
claude.pache at gmail.com
Sun Aug 5 03:07:36 UTC 2018
> Le 5 août 2018 à 04:50, Claude Pache <claude.pache at gmail.com> a écrit :
>
>
>
>> Le 5 août 2018 à 01:43, Michael Theriot <michael.lee.theriot at gmail.com <mailto:michael.lee.theriot at gmail.com>> a écrit :
>>
>> Try `Number.prototype.valueOf.call(obj)`: it will throw a TypeError if and only if `obj` has no [[NumberData]] internal slot. Ditto for String, Boolean and Symbol.
>>
>> I already mention this and demonstrate why it is not sufficient in my example.
>>
>> Reiterated plainly:
>> ```js
>> JSON.stringify(Reflect.construct(Number, [], Number)); // "0"
>> JSON.stringify(Reflect.construct(Number, [], String)); // TypeError
>> JSON.stringify(Reflect.construct(Number, [], Object)); // null
>> ```
>
> Per spec, the three expressions should produce `"0"`, as the three objects have a [[NumberData]] internal slot (step 4 of [1]). I guess there is some discrepancy between implementation and spec for those exotic edge cases?
>
> [1]: https://tc39.github.io/ecma262/#sec-serializejsonproperty <https://tc39.github.io/ecma262/#sec-serializejsonproperty>
>
> —Claude
>
I see that there is a difference in this algorithm between the current spec and ES 2015 for that particular step. From my tests, current stable Firefox and Safari (other browsers not tested) still follow the old algorithm.
—Claude
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180805/4476a7e6/attachment.html>
More information about the es-discuss
mailing list