javascript 中的自动转换:当像 stringvar = 1+stringvar 那样完成时,难道不应该将字符串转换为数字吗?
Autoconversion in javascript: Isn't it supposed to convert a string to a number when done like stringvar = 1+stringvar?
(Firefox32, Win 7)
使用暂存器时:
var a = "2";
a = 1 - 1 + a;
console.log(a, typeof a);
a = 1 + a - 1;
console.log(a, typeof a);
在控制台中引导至:
"02" "string"
101 "number"
我是不是做错了什么?
我确信我经常以这种方式将字符串转换为数字,而且它很有效。 (虽然我更喜欢使用显式转换函数,但由于它如此完美地工作了这么长时间,...:/)
为什么第三行是数字?但不是第二个?为什么是 101?
这就是字符串连接的工作原理。
// Number + String -> concatenation
5 + "foo" // "5foo"
您可以使用 Unary Plus operator (+
) 来避免调用 parseInt
:
a = 1 + +a;
Unary plus (+
)
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. 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.
Isn't it supposed to convert a string to a number when done like stringvar = 1+stringvar?
没有。 :-) 如果 either 操作数是字符串,则它是字符串连接,而不是加法。这由步骤 7 中的 §11.6.1 of the spec 定义:
- Let lref be the result of evaluating AdditiveExpression.
- Let lval be GetValue(lref).
- Let rref be the result of evaluating MultiplicativeExpression.
- Let rval be GetValue(rref).
- Let lprim be ToPrimitive(lval).
- Let rprim be ToPrimitive(rval).
- If Type(lprim) is String or Type(rprim) is String, then
- Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)
- Return the result of applying the addition operation to ToNumber(lprim) and ToNumber(rprim). See the Note below 11.6.3.
(我强调"or")
关于你的第三个例子,1 + a - 1
其中 a
是 "02"
:
1 + "02" - 1
加法(二元+
)和减法(二元-
)运算符具有相同的优先级,并且都是从左到右结合的,因此执行如下:
(1 + "02") - 1 => "102" - 1 => 101
+
是字符串连接,但由于 subtraction operator -
总是 与数字一起使用,它会将 "102"
强制为 102
从中减去 1
得到 101
.
(Firefox32, Win 7)
使用暂存器时:
var a = "2";
a = 1 - 1 + a;
console.log(a, typeof a);
a = 1 + a - 1;
console.log(a, typeof a);
在控制台中引导至:
"02" "string"
101 "number"
我是不是做错了什么?
我确信我经常以这种方式将字符串转换为数字,而且它很有效。 (虽然我更喜欢使用显式转换函数,但由于它如此完美地工作了这么长时间,...:/)
为什么第三行是数字?但不是第二个?为什么是 101?
这就是字符串连接的工作原理。
// Number + String -> concatenation 5 + "foo" // "5foo"
您可以使用 Unary Plus operator (+
) 来避免调用 parseInt
:
a = 1 + +a;
Unary plus (
+
)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. 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.
Isn't it supposed to convert a string to a number when done like stringvar = 1+stringvar?
没有。 :-) 如果 either 操作数是字符串,则它是字符串连接,而不是加法。这由步骤 7 中的 §11.6.1 of the spec 定义:
- Let lref be the result of evaluating AdditiveExpression.
- Let lval be GetValue(lref).
- Let rref be the result of evaluating MultiplicativeExpression.
- Let rval be GetValue(rref).
- Let lprim be ToPrimitive(lval).
- Let rprim be ToPrimitive(rval).
- If Type(lprim) is String or Type(rprim) is String, then
- Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)
- Return the result of applying the addition operation to ToNumber(lprim) and ToNumber(rprim). See the Note below 11.6.3.
(我强调"or")
关于你的第三个例子,1 + a - 1
其中 a
是 "02"
:
1 + "02" - 1
加法(二元+
)和减法(二元-
)运算符具有相同的优先级,并且都是从左到右结合的,因此执行如下:
(1 + "02") - 1 => "102" - 1 => 101
+
是字符串连接,但由于 subtraction operator -
总是 与数字一起使用,它会将 "102"
强制为 102
从中减去 1
得到 101
.