View Single Post
  #1 (permalink)  
Old February 8th, 2017, 04:53 AM
tingxuelouwq tingxuelouwq is offline
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2017
Location: Beijing, China
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Post 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.