为什么最好不要在JavaScript中将数字创建为对象?
在javascript中,尽量不要将数字 转换为对象, 因为数字不能与对象 进行比较,甚至对象 也不能与对象进行比较。
示例
在以下示例中,将提供的数字20分配给变量'x'和变量'y'。将两个变量相互比较时,将显示布尔 值“true”,如输出所示。
<html>
<body>
<script>
var x = 500;
var y = (500);
document.write((x===y));
document.write("</br>");
document.write(typeof(x));
document.write("</br>");
document.write(typeof(y));
</script>
</body>
</html>输出结果
true number number
示例
在以下示例中,变量“y”从数字 变为对象 ,然后与变量“x”比较时,布尔 值false 显示为输出,如输出所示。
<html>
<body>
<script>
var x = 500;
var y = new Number(500);
document.write((x===y));
document.write("</br>");
document.write(typeof(x));
document.write("</br>");
document.write(typeof(y));
</script>
</body>
</html>输出结果
false number object
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短