javascript 强制编号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15865346/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:22:19  来源:igfitidea点击:

Coerce to number

javascript

提问by Randomblue

I have often seen the trick

我经常看到这个伎俩

after = +after;

to coerce the variable afterto a number. Reading through the Node.JS sourceI found another method:

将变量强制after为一个数字。阅读Node.JS 源代码,我发现了另一种方法:

after *= 1; // coalesce to number or NaN

Are the two methods strictly equivalent in their behaviour?

这两种方法的行为是否严格等效?

回答by Fabrício Matté

Yes. Both Unary Operator +and Multiplicative Operatorssuch as *(called from Compound Assignmentop=) invoke the internal ToNumberalgorithm.

是的。两个一元运算符+乘法运算符,例如*(来自称为复合赋值op=)调用内部ToNumber算法。

You can even use a 3rd option by statically calling the Numberconstructor:

您甚至可以通过静态调用Number构造函数来使用第三个选项:

after = Number(after);

回答by christopher

After a quick google to make sure my suspicions were true, I came to this conclusion. Using the +operator to convert to a number is faster, because no mathematical operations occur after type-casting, whereas using the *=approach means that after afteris converted, it will be multiplied by 1.

在快速谷歌以确保我的怀疑是真的之后,我得出了这个结论。使用+运算符转换为数字更快,因为类型转换后不会发生数学运算,而使用该*=方法意味着after转换后会乘以1

回答by Wilco

Note: In some instances after = after-0invokes different behaviour than after = after+0. I've noticed it with dates.

注意:在某些情况下,after = after-0调用与after = after+0. 我已经注意到它与日期。

This is tested in Chrome v39 only:

这仅在 Chrome v39 中测试:

var date = new Date(2000,0,1);
date += date; //"Sat Jan 01 2000 00:00:00 GMT+0000 (GMT Standard Time)Sat Jan 01 2000 00:00:00 GMT+0000 (GMT Standard Time)"
var date2 = new Date(2000,0,1);
date2 + 0; //"Sat Jan 01 2000 00:00:00 GMT+0000 (GMT Standard Time)0"
date2 - 0; //946684800000
date2 * 1; //946684800000

I don't know what is defined in the JS spec, but with dates, because both the date and the number can be cast to a string, and the + operator works on a string, then Chrome goes with a string concatenation. Because the - operator has no string equivalent, it falls back to number comparison.

我不知道 JS 规范中定义了什么,但是使用日期,因为日期和数字都可以转换为字符串,并且 + 运算符适用于字符串,然后 Chrome 使用字符串连接。因为 - 运算符没有等效的字符串,所以它回退到数字比较。

I've found this useful when coercing dates into numbers for comparisons

我发现这在将日期强制转换为数字进行比较时很有用

回答by Timmmm

Yes, however note that it is only the unary+ operator that does this. I.e. 10 + "10"will give you "1010".

是的,但是请注意,只有一元+ 运算符才能执行此操作。即10 + "10"会给你"1010"

A perhaps less error prone option is to use what asm.js does:

一个可能不太容易出错的选项是使用 asm.js 所做的:

10 + ("10"|0)

Although on the down-side it does require the brackets. It should be the fastest option in any case (probably equal to unary +).

尽管在不利方面,它确实需要支架。在任何情况下它都应该是最快的选项(可能等于一元 +)。