JavaScript 如何处理 ++ 操作员?

发布于 2024-07-10 07:23:47 字数 370 浏览 6 评论 0原文

JavaScript 对对象进行时髦的自动转换:

var o = {toString: function() {return "40"; }};
print(o + o);
print((o+1)+o);
print((o*2) + (+o));

将打印:

4040
40140
120

这是因为 +,如果任何参数是对象/字符串,将尝试将所有参数转换为字符串,然后连接它们。 如果所有参数都是数字,它将它们加在一起。 * 和一元 + 使用 toString (以及 valueOf,此处未显示)将对象转换为数字。

JavaScript 对 ++ 运算符有何作用?

JavaScript does funky automatic conversions with objects:

var o = {toString: function() {return "40"; }};
print(o + o);
print((o+1)+o);
print((o*2) + (+o));

will print:

4040
40140
120

This is because +, if any of the arguments are objects/strings, will try to convert all the arguments to strings then concatenate them. If all arguments are numbers, it adds them together. * and unary + convert objects to numbers using toString (as well as valueOf, not shown here).

What does JavaScript do for the ++ operator?

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

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

发布评论

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

评论(3

谜泪 2024-07-17 07:23:47

来自 ECMAScript 语言规范

11.3 后缀表达式

语法

后缀表达式:

  • LeftHandSideExpression
  • LeftHandSideExpression [此处没有行终止符] ++
  • LeftHandSideExpression [此处没有 LineTerminator] --

11.3.1 Postfix 增量运算符

生产PostfixExpression:
LeftHandSideExpression [否
LineTerminator 在这里] ++
被评估
如下:

  1. 计算 LeftHandSideExpression。
  2. 调用 GetValue(Result(1))。
  3. 调用 ToNumber(Result(2))。
  4. 使用与 + 运算符相同的规则将值 1 添加到 Result(3)
    (第 11.6.3 节)。
  5. 调用 PutValue(Result(1), Result(4))。
  6. 返回结果(3)。

这是 postInc 如何工作的伪 javascript 代码:

function postInc(a) {
  var x = +a; // Converts a to a number, Section 11.4.6 Unary + Operator
  a = x + 1;
  return x;
}

编辑:正如 mikesamuel 所说:它不是 parseInt。 已更新以反映这一点。

From ECMAScript Language Specification

11.3 Postfix Expressions

Syntax

PostfixExpression :

  • LeftHandSideExpression
  • LeftHandSideExpression [no LineTerminator here] ++
  • LeftHandSideExpression [no LineTerminator here] --

11.3.1 Postfix Increment Operator

The production PostfixExpression :
LeftHandSideExpression [no
LineTerminator here] ++
is evaluated
as follows:

  1. Evaluate LeftHandSideExpression.
  2. Call GetValue(Result(1)).
  3. Call ToNumber(Result(2)).
  4. Add the value 1 to Result(3), using the same rules as for the + operator
    (section 11.6.3).
  5. Call PutValue(Result(1), Result(4)).
  6. Return Result(3).

This is pseudo javascript code of how postInc works:

function postInc(a) {
  var x = +a; // Converts a to a number, Section 11.4.6 Unary + Operator
  a = x + 1;
  return x;
}

Edit: As mikesamuel said: it's not parseInt. Updated to reflect that.

幸福%小乖 2024-07-17 07:23:47

下面的代码很好地说明了这一点:

var a = {toString: function() {return "40"; }};
nl(typeof a);
nl(typeof +a);
nl(typeof a);
nl(typeof (a++));
nl(a);
nl(typeof a);

输出为:

object
number
object
number
41
number

一元加将对象转换为数字并且不修改它。 a++ 首先将对象转换为数字,然后返回该数字,然后递增该数字,将值存储在 a 中。

这与另一种可能的解决方案相反,其中 a++ 首先返回对象,然后转换为数字并递增。

The following code illustrates this well:

var a = {toString: function() {return "40"; }};
nl(typeof a);
nl(typeof +a);
nl(typeof a);
nl(typeof (a++));
nl(a);
nl(typeof a);

The output is:

object
number
object
number
41
number

Unary plus converts the object to a number and doesn't modify it. a++ first converts the object to a number, then returns that number, and then increments the number, storing the value in a.

This is opposed to another possible solution, where a++ would first return the object, and then do the conversion to a number and incrementation.

小情绪 2024-07-17 07:23:47

++ 运算符执行“toNumber”转换(基本上是类型规则和 valueOf 函数的组合)。 基本上对于任何解析表达式

 resolveExpression++

JS 引擎采取的步骤是

 <temp> = toNumber(resolveExpression);
 resolveExpression = <temp> + 1;
 <result> = <temp>

对于非原子解析表达式,例如: base.resolve++base["resolve"]++ 等。 base 仅解析一次,然后重复使用。 在任何理智的情况下,这都是无关紧要的,但是如果要递增的值是一个具有更改基础对象的 valueOf 实现的对象,那么这一点很重要。

例如。

base = {};
base.value = {valueOf:function(){base = {}; return 5;}}
base.value++;

The ++ operator does a "toNumber" conversion (basically a combination of type rules and the valueOf function). Basically for any resolve expression

 resolveExpression++

The steps taken by the JS engine are

 <temp> = toNumber(resolveExpression);
 resolveExpression = <temp> + 1;
 <result> = <temp>

For non-atomic resolve expressions, eg. base.resolve++ or base["resolve"]++, etc. base is resolved only once and then reused. In any sane case this is irrelevant, however it's important if the value being incremented is an object with a valueOf implementation that changes the base object.

eg.

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