パンツないな興奮 - Javascript stuff 1

Posted by Aaron Patterson on January 19, 2008

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.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Gavin Kistner Tue, 12 Feb 2008 12:31:40 PST

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

Comments

Check Spelling
Activate Spell Check while Typing