Automatic javascript type coercion
javascript
Solution
The addition operator coerces its arguments to strings in step 7:
- If Type(lprim) is String or Type(rprim) is String, then
- Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)
The ToString operation has special rules for numbers, detailed under "ToString Applied to the Number Type".
It appears that the abstract operation `ToString(number)` does not ever use `Number.prototype.toString`. In fact, ti's the other way around: the default `Number.prototype.toString` function employs the abstract numeric ToString algorithm. Thus, it's not possible to override the default behavior for the stringification of numbers during type coercion in ECMAScript 5, because there's no way to alter the language-defined ToString operation.
ToString does use the target's `toString` method when coercing objects to strings, but not number primitive values.
Problem
This has become more of a pondering about how javascript works, than an actual problem to fix. In the case of a statement like ``` var str = 9 + " some words here"; ``` The resultant str var would contain the value "9 some words here". My question is what function does javascript use to automatically coerce the Number object '9' into a string to be concatenated with the String object " some words here", and is this function changeable/overrideable. This started from me needing to output single digits with a preceding 0 on a page. This was easy enough to accomplish with a quick prototype function on the Number object ``` Number.prototype.SpecialFormat = function() { if(this < 10) { return "0" + this; } else { return this.toString(); } }; ``` And call it with a simple `(9).SpecialFormat() + " words here";` But that got me wondering if I could overwrite the toString function on a Number with a ``` Number.prototype.toString = function() { if(this < 10) { return "0" + this; } else { return this; } }; ``` , and just let javascripts automatic conversion handle it for me, so I could use a standard `9 + " words here";` to get the same result "09 words here". This did not just implicitly work, I had to end up adding .toString to the 9 `(9).toString() + " words here"` (which upon taking a further look, would have resulted in some infinite loops). So is there a way to override the built in functionality of a javascript native type ? *Note: I realize this is likely a 'worst idea ever'