本文共 913 字,大约阅读时间需要 3 分钟。
javascript运算符
Every complex statement will introduce precedence problems.
每个复杂的语句都会引入优先级问题。
Take this:
拿着这个:
const a = 1 * 2 + 5 / 2 % 2
The result is 2.5, but why? What operations are executed first, and which need to wait?
结果是2.5,但是为什么呢? 首先执行哪些操作,哪些需要等待?
Some operations have more precedence than the others. The precedence rules are listed in this table:
一些操作比其他操作具有更高的优先级。 优先级规则在此表中列出:
Operator | Description |
---|---|
- + ++ -- | unary operators, increment and decrement |
* / % | multiply/divide |
+ - | addition/subtraction |
= += -= *= /= %= **= | assignments |
操作员 | 描述 |
---|---|
- + ++ -- | 一元运算符,递增和递减 |
* / % | 乘/除 |
+ - | 加/减 |
= += -= *= /= %= **= | 作业 |
Operations on the same level (like +
and -
) are executed in the order they are found
同一级别的操作(如+
和-
)将按找到的顺序执行
Following this table, we can solve this calculation:
遵循此表,我们可以解决此计算问题:
const a = 1 * 2 + 5 / 2 % 2const a = 2 + 5 / 2 % 2const a = 2 + 2.5 % 2const a = 2 + 0.5const a = 2.5
翻译自:
javascript运算符
转载地址:http://hqqgb.baihongyu.com/