p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional JavaScript for Web Developers 3rd edition (http://p2p.wrox.com/forumdisplay.php?f=669)
-   -   Number Conversions (http://p2p.wrox.com/showthread.php?t=99364)

tingxuelouwq February 8th, 2017 04:53 AM

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.


All times are GMT -4. The time now is 03:39 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.