该valueOf()
函数接受一个Number对象,并返回由给定对象包装的数字(原始)。
其语法如下
num.valueOf(num);
<html> <head> <title>JavaScript Example</title> </head> <body> <script type="text/javascript"> var num = 215; result = num.toString(16); document.write("Result: " + result); </script> </body> </html>
输出结果
Result: d7
<html> <head> <title>JavaScript Example</title> </head> <body> <script type="text/javascript"> var num = 215; result = num.toString(16); document.write("Result: " + result); document.write("<br>"); document.write("Result: " + num.valueOf(0)); document.write("<br>"); document.write("Result: " + num.valueOf(-16)); document.write("<br>"); document.write("Result: " + num.valueOf()); document.write("<br>"); var dat = 0/0; document.write("Result: " + dat.valueOf(16)); </script> </body> </html>
输出结果
Result: d7 Result: 215 Result: 215 Result: 215 Result: NaN