Javascript new Number() 与 Number()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4719320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
new Number() vs Number()
提问by Tom Tucker
What is the difference between new Number()
and Number()
? I get that new Number()
creates a Number
object and Number()
is just a function, but when should I call which, and why?
new Number()
和 和有Number()
什么区别?我知道它new Number()
创建了一个Number
对象并且Number()
只是一个函数,但是我什么时候应该调用 which,为什么?
On a related note, Mozilla says:
在相关说明中,Mozilla 说:
Do not use a Boolean object to convert a non-boolean value to a boolean value. Instead, use Boolean as a function to perform this task.
x = Boolean(expression); // preferred x = new Boolean(expression); // don't use
不要使用布尔对象将非布尔值转换为布尔值。相反,使用 Boolean 作为函数来执行此任务。
x = Boolean(expression); // preferred x = new Boolean(expression); // don't use
Why is that? I thought the results were the same?
这是为什么?我以为结果是一样的?
采纳答案by David Tang
Boolean(expression)
will simply convert the expression into a boolean primitive value, while new Boolean(expression)
will create a wrapper objectaround the converted boolean value.
Boolean(expression)
将简单地将表达式转换为布尔原始值,同时new Boolean(expression)
将围绕转换后的布尔值创建一个包装对象。
The difference can be seen with this:
可以看出不同之处:
// Note I'm using strict-equals
new Boolean("true") === true; // false
Boolean("true") === true; // true
And also with this (thanks @hobbs):
还有这个(感谢@hobbs):
typeof new Boolean("true"); // "object"
typeof Boolean("true"); // "boolean"
Note:While the wrapper object will get converted to the primitive automatically when necessary (and vice versa), there is only one case I can think of where you would want to use new Boolean
, or any of the other wrappers for primitives - if you want to attach properties to a single value. E.g:
注意:虽然包装器对象会在必要时自动转换为原语(反之亦然),但我只能想到一种情况,你想在哪里使用new Boolean
,或任何其他用于原语的包装器 - 如果你想将属性附加到单个值。例如:
var b = new Boolean(true);
b.relatedMessage = "this should be true initially";
alert(b.relatedMessage); // will work
var b = true;
b.relatedMessage = "this should be true initially";
alert(b.relatedMessage); // undefined
回答by ?ime Vidas
new Number( x )
creates a new wrapper object. I don't think that there is a valid reason to ever use this.
创建一个新的包装对象。我不认为有充分的理由使用它。
Number( x )
converts the passed argument into a Number value. You can use this to cast some variable to the Number type. However this gets the same job done:
将传递的参数转换为 Number 值。您可以使用它来将一些变量转换为 Number 类型。但是,这可以完成相同的工作:
+x
Generally:
一般来说:
You don't need those:
你不需要那些:
new Number()
new String()
new Boolean()
You can use those for casting:
您可以将它们用于铸造:
Number( value )
String( value )
Boolean( value )
However, there are simpler solutions for casting:
但是,有更简单的铸造解决方案:
+x // cast to Number
'' + x // cast to String
!!x // cast to Boolean
回答by T.J. Crowder
Always worth consulting the spec; from Section 15.7.1:
始终值得咨询规范;来自第 15.7.1 节:
When
Number
is called as a function rather than as a constructor, it performs a type conversion.
当
Number
作为函数而不是构造函数调用时,它执行类型转换。
Similarly, using Boolean
as a function (15.6.1):
同样,Boolean
用作函数(15.6.1):
When Boolean is called as a function rather than as a constructor, it performs a type conversion.
当 Boolean 作为函数而不是构造函数调用时,它执行类型转换。
...which means that you consult Section 9.2 ("ToBoolean"):
...这意味着您查阅第 9.2 节(“ToBoolean”):
The abstract operation ToBoolean converts its argument to a value of type Boolean according to Table 11:
Undefined
=false
Null
=false
Boolean
= The result equals the input argument (no conversion).Number
= The result is false if the argument is +0, ?0, or NaN; otherwise the result is true.String
= The result is false if the argument is the empty String (its length is zero); otherwise the result is true.Object
=true
抽象操作 ToBoolean 根据表 11 将其参数转换为 Boolean 类型的值:
Undefined
==false
Null
=false
Boolean
结果等于输入参数(无转换)。Number
= 如果参数为 +0、?0 或 NaN,则结果为 false;否则结果为真。String
= 如果参数为空字符串(其长度为零),则结果为假;否则结果为真。Object
=true
The difference between new Boolean(value)
and Boolean(value)
is basically that the former returns an object, but the latter returns a primitive per the above. This matters, because objects are truthy:
new Boolean(value)
和之间的区别Boolean(value)
基本上是前者返回一个对象,而后者按照上述返回一个原语。这很重要,因为对象是真实的:
var b = new Boolean(false);
display(b); // Displays "false"
if (b) {
display("true"); // This is the path that gets taken, displaying "true"
}
else {
display("false"); // This path does NOT get taken
}
Live example...whereas you almost always want booleans for the purpose of testing them.
现场示例...而您几乎总是想要布尔值来测试它们。