Number Conversions
On page 39, when it comes to transform Object into Number, the book said:" When applied to objects, the valuesOf() method is called and the returned value is converted based on the previously described rules. If that conversion results in NaN, the toString() method is called and the rules for converting strings are applied." However, I found if I write code as follow:
var obj = {
valueOf: function() {
return NaN;
},
toString: function() {
return -1;
}
}
document.write(Number(obj)); // NaN
If I comment out valueOf() method, then the result will be -1. That means when applied Number() to objects, the valueOf() method is called and the returned value is converted based on the previously described rules. If that conversion results in NaN, then returns NaN, but not calls toString() method. If there is no valueOf() method, then the toString() method is called.
|