パンツないな興奮 - Javascript stuff

While writing RKelly (a pure ruby javascript interpreter), I've run across weird cases in the ECMA spec that I didn't know about before. For example, when coercing an object into a number, first the "valueOf" property is checked. That seemed to make sense to me. But then the spec says that if there is no "valueOf" property, to check the "toString" property and try to coerce that in to a number.

So, this bit of code:

var x = new Object;
x.valueOf = function() { return 11; };
x++;

is equivalent to this bit of code:

var x = new Object;
x.stringValue = function() { return "11"; }
x++;

That is some underpants-free excitement.

One Comment

  1. Posted February 12, 2008 at 12:31 pm | Permalink

    Wow…I had no idea/remembrance that was in the spec, and I’ve read through it twice. Thanks for sharing :)

Post a Comment

Your email is never shared. Required fields are marked *

*
*
Check Spelling
Activate Spell Check while Typing