+ javascript 中表达式之前的运算符:它有什么作用?

发布于 2024-12-18 17:28:25 字数 393 浏览 2 评论 0原文

我正在仔细阅读 underscore.js 库,我发现了一些我没有找到的东西之前遇到过:

if (obj.length === +obj.length) { ... }

+ 运算符在那里做什么?对于上下文,这里有一个直接链接到该部分文件。

I was perusing the underscore.js library and I found something I haven't come across before:

if (obj.length === +obj.length) { ... }

What is that + operator doing there? For context, here is a direct link to that part of the file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

诠释孤独 2024-12-25 17:28:25

在 JavaScript 中,一元 + 运算符可用于将值转换为数字。 Underscore 似乎正在测试 .length 属性是否为数字,否则它将不等于自身转换为数字。

The unary + operator can be used to convert a value to a number in JavaScript. Underscore appears to be testing that the .length property is a number, otherwise it won't be equal to itself-converted-to-a-number.

顾忌 2024-12-25 17:28:25

根据MDN

一元加运算符位于其操作数之前并计算其结果
操作数,但尝试将其转换为数字(如果不是)
已经。例如,y = +x 获取 x 的值并将其分配给
y;也就是说,如果 x 为 3,则 y 将得到值 3,而 x 将保留
值 3;但如果 x 是字符串“3”,y 也会得到该值
3. 虽然一元否定 (-) 也可以转换非数字,但一元加是将某些内容转换为数字的最快且首选的方式
号,因为它不执行任何其他操作
数字。它可以转换整数和浮点数的字符串表示形式,
以及非字符串值 true、false 和 null。整数在
支持十进制和十六进制(“0x”前缀)格式。
支持负数(但不支持十六进制)。如果不能的话
解析特定值,它将计算为 NaN。

According to MDN:

The unary plus operator precedes its operand and evaluates to its
operand but attempts to converts it into a number, if it isn't
already. For example, y = +x takes the value of x and assigns that to
y; that is, if x were 3, y would get the value 3 and x would retain
the value 3; but if x were the string "3", y would also get the value
3. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a
number, because it does not perform any other operations on the
number. It can convert string representations of integers and floats,
as well as the non-string values true, false, and null. Integers in
both decimal and hexadecimal ("0x"-prefixed) formats are supported.
Negative numbers are supported (though not for hex). If it cannot
parse a particular value, it will evaluate to NaN.

落花随流水 2024-12-25 17:28:25

这是确保 obj.length 是数字而不是潜在字符串的一种方法。这样做的原因是,如果长度(无论出于何种原因)是字符串变量,例如“3”,则 === 将失败。

It's a way of ensuring that obj.length is a number rather than a potential string. The reason for this is that the === will fail if the length (for whatever reason) is a string variable, e.g. "3".

节枝 2024-12-25 17:28:25

检查 obj.length 是否属于 number 类型是一个很好的技巧。您会看到,+ 运算符可用于字符串强制转换。例如:

alert(+ "3" + 7); // alerts 10

这是可能的,因为 + 运算符将字符串 "3" 强制转换为数字 3。因此结果是 10 而不是 "37"

另外,JavaScript 有两种类型的相等和不等运算符:

  1. 严格相等和不相等(例如 3 === "3" 表示 false)。
  2. 正常的相等和不相等(例如 3 == "3" 表示 true)。

严格的平等和不平等并不强制价值。因此数字 3 不等于字符串 "3"。正常的平等和不平等确实会强制价值。因此数字 3 等于字符串 "3"

现在,上面的代码只是使用+运算符将obj.length强制转换为数字,并严格检查强​​制转换前后的值是否相同(即>obj.length 类型为 number)。它在逻辑上等价于以下代码(只是更简洁):

if (typeof obj.length === "number") {
    // code
}

It's a nice hack to check whether obj.length is of the type number or not. You see, the + operator can be used for string coercion. For example:

alert(+ "3" + 7); // alerts 10

This is possible because the + operator coerces the string "3" to the number 3. Hence the result is 10 and not "37".

In addition, JavaScript has two types of equality and inequality operators:

  1. Strict equality and inequality (e.g. 3 === "3" expresses false).
  2. Normal equality and inequality (e.g. 3 == "3" expresses true).

Strict equality and inequality doesn't coerce the value. Hence the number 3 is not equal to the string "3". Normal equality and inequality does coerce the value. Hence the number 3 is equal to the string "3".

Now, the above code simply coerces obj.length to a number using the + operator, and strictly checks whether the value before and after the coercion are the same (i.e. obj.length of the type number). It's logically equivalent to the following code (only more succinct):

if (typeof obj.length === "number") {
    // code
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文