+ javascript 中的一元运算符
+ unary operator in javascript
我已经读过这个主题了:
Explain +var and -var unary operator in javascript
但我仍然无法理解这个简单的代码:
var a = 3;
console.log(-a); // -3
console.log(+a); // 3
a = -a;
console.log(a); // -3
console.log(+a); // -3
"The unary negation operator precedes its operand and negates it."
"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."
但我还是不明白为什么第一次 console.log(+a) return 3。
but i still can't figure why console.log(+a) return 3 the first time.
此时 a
的值为 3
。
上一行 -a
采用 a
的 值,取反并将其传递给 console.log
。它 不会 将更改后的值分配回 a
。
我已经读过这个主题了: Explain +var and -var unary operator in javascript
但我仍然无法理解这个简单的代码:
var a = 3;
console.log(-a); // -3
console.log(+a); // 3
a = -a;
console.log(a); // -3
console.log(+a); // -3
"The unary negation operator precedes its operand and negates it."
"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."
但我还是不明白为什么第一次 console.log(+a) return 3。
but i still can't figure why console.log(+a) return 3 the first time.
此时 a
的值为 3
。
上一行 -a
采用 a
的 值,取反并将其传递给 console.log
。它 不会 将更改后的值分配回 a
。