为什么隐式强制加法总是产生一个字符串? [英] Why does implicit coercion for addition always produce a string?

查看:57
本文介绍了为什么隐式强制加法总是产生一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果仅第二个操作数是一个字符串,则输出是一个字符串:

If only the second operand in addition is a string then the output is a string:

let a = 1 + '2';
console.log(typeof a);  // string

如果只有第一个操作数是字符串,则输出仍然是字符串:

And if, instead, only the first operand is a string, then the output is still a string:

let b = '1' + 2;
console.log(typeof b);  // string

我猜想会有某种论点优先.为什么有这个数学函数默认为带有混合类型参数的非数字输出的原因?

I was guessing that there would be some kind of argument precedence. Is there a reason why this mathematical function defaults to a non-numerical output with mixed-type arguments?

推荐答案

当单个运算符是表达式的一部分时,该表达式从左到右执行,但是在JavaScript中,如果有任何与运算符是一个字符串,另一个将转换为字符串-哪个无关紧要.这是因为+操作可能意味着字符串加法(连接)或数学加法.当一个操作数是一个字符串时,JavaScript运行时会正确地假设+应该表示添加字符串,因为该字符串可能包含一个非数字值,并且用非数字值进行数学运算是有问题的,至少可以这样说.

When a single operator is part of an expression, the expression is performed left to right, but in JavaScript, if any one of the operands used with the + operator is a string, the other one will be converted to a string - it doesn't matter which one. This is because the + operation can mean string addition (concatenation) or mathematical addition. When one operand is a string, the JavaScript runtime correctly assumes that the + should mean string addition because the string could contain a non-numeric value and doing math with non-numeric values is problematic, to say the least.

在串联之前,您需要对非字符串进行转换.这可以通过多种方式完成:

You would need to do a conversion on the non-string before the concatenation occurs. This can be done in a number of ways:

console.log(1 + +"2");              // prepending a + to a string attempts to convert to a number

// Note that with all of the following there is a nested function call being performed
// and these functions take an argument, which requires () for the argument to be passed.
// Because of the nested (), that function call is performed first and the result of the 
// function call is returned to the expression.

console.log(1 + parseInt("2.4"));   // parse the integer portion of the string into a number
console.log(1 + parseFloat("2.4")); // parse the floating point number in the string into a number
console.log(1 + Number("2"));       // convert the string into a number

基本运算符优先级是:

  • 肢体感觉
  • 指数
  • 乘法
  • 部门
  • 添加
  • 减法

因此,在以下示例中,您可以看到这种情况:

So, in the following examples, you can see that happening:

// Here, the order of operations will be:

  // Parenthesis:    (2/2) === 1
  // Multiplication: 10 * 1 === 10
  // Addition:       1 + 10 === 11
  // Subtraction:    11 - 3 === 8
console.log(1 + 10 * (2 / 2) - 3);

// But here, because of the string, the order of operations will be:

  // Parenthesis:    (2/2) === 1
  // Multiplication: 10 * 1 === 10
  // Addition:       "1" + 10 === "110" (one operand is a string, so the other converts to a string)
  // Subtraction:    "110" - 3 === 107  (the string "110" will be implicitly converted to a number
  //                                     becuase the - sign  only has one meaning in a mathmatical 
  //                                     expression)
console.log("1" + 10 * (2 / 2) - 3);

请参见 JavaScript运算符优先级 ,以获取操作符及其优先级的完整列表.

See JavaScript Operator Precedence for a full list of operators and their precedence.

请参见 一元运算符 ,了解如何将其用于转换为数字.

See Unary + Operator for how it is used to convert to a number.

还要注意,这里我们不是在讨论参数"(参数是传递给方法或函数的参数).这些是带有 运算符 的表达式中的 操作数 .

Also note that we're not talking about "arguments" here (an argument is what is passed to a method or function). These are operands in an expression with operators.

这篇关于为什么隐式强制加法总是产生一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆