You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mathematically, the exponentiation is defined for non-integer numbers as well. For example, a square root is an exponentiation by `1/2`:
69
+
在数学上,求幂的定义也适用于非整数。例如,平方根是以 `1/2` 为单位的求幂:
70
70
71
71
```js run
72
-
alert( 4 ** (1/2) ); // 2 (power of 1/2 is the same as a square root)
73
-
alert( 8 ** (1/3) ); // 2 (power of 1/3 is the same as a cubic root)
72
+
alert( 4 ** (1/2) ); // 2(1/2 次方与平方根相同)
73
+
alert( 8 ** (1/3) ); // 2(1/3 次方与立方根相同)
74
74
```
75
75
76
76
77
-
## String concatenation with binary +
77
+
## 用二元运算符 + 连接字符串
78
78
79
-
Let's meet features of JavaScript operators that are beyond school arithmetics.
79
+
我们来看一些学校算术未涉及的 JavaScript 运算符的特性。
80
80
81
81
通常,加号 `+` 用于求和。
82
82
@@ -96,23 +96,23 @@ alert( '1' + 2 ); // "12"
96
96
alert( 2 + '1' ); // "21"
97
97
```
98
98
99
-
See, it doesn't matter whether the first operand is a string or the second one.
99
+
你看,第一个运算元和第二个运算元,哪个是字符串并不重要。
100
100
101
-
Here's a more complex example:
101
+
下面是一个更复杂的例子:
102
102
103
103
```js run
104
-
alert(2 + 2 + '1' ); // "41" and not "221"
104
+
alert(2 + 2 + '1' ); // "41",不是 "221"
105
105
```
106
106
107
-
Here, operators work one after another. The first `+` sums two numbers, so it returns `4`, then the next `+` adds the string `1` to it, so it's like `4 + '1' = 41`.
The binary `+`is the only operator that supports strings in such a way. Other arithmetic operators work only with numbers and always convert their operands to numbers.
上面这个例子,`(a = b + 1)` 的结果是赋给 `a` 的值(也就是 `3`)。然后该值被用于进一步的运算。
236
236
237
-
Funny code, isn't it? We should understand how it works, because sometimes we see it in JavaScript libraries.
237
+
有趣的代码,不是吗?我们应该了解它的工作原理,因为有时我们会在 JavaScript 库中看到它。
238
238
239
-
Although, please don't write the code like that. Such tricks definitely don't make code clearer or readable.
239
+
不过,请不要写这样的代码。这样的技巧绝对不会使代码变得更清晰或可读。
240
240
241
-
### Chaining assignments
241
+
### 链式赋值(Chaining assignments)
242
242
243
-
Another interesting feature is the ability to chain assignments:
243
+
另一个有趣的特性是链式赋值:
244
244
245
245
```js run
246
246
let a, b, c;
@@ -254,49 +254,49 @@ alert( b ); // 4
254
254
alert( c ); // 4
255
255
```
256
256
257
-
Chained assignments evaluate from right to left. First, the rightmost expression `2 + 2` is evaluated and then assigned to the variables on the left: `c`, `b` and `a`. At the end, all the variables share a single value.
Once again, for the purposes of readability it's better to split such code into few lines:
259
+
同样,出于可读性,最好将这种代码分成几行:
260
260
261
261
```js
262
262
c = 2 + 2;
263
263
b = c;
264
264
a = c;
265
265
```
266
-
That's easier to read, especially when eye-scanning the code fast.
266
+
这样可读性更强,尤其是在快速浏览代码的时候。
267
267
268
-
## Modify-in-place
268
+
## 原地修改
269
269
270
-
We often need to apply an operator to a variable and store the new result in that same variable.
270
+
我们经常需要对一个变量做运算,并将新的结果存储在同一个变量中。
271
271
272
-
For example:
272
+
例如:
273
273
274
274
```js
275
275
let n = 2;
276
276
n = n + 5;
277
277
n = n * 2;
278
278
```
279
279
280
-
This notation can be shortened using the operators `+=` and `*=`:
280
+
可以使用运算符 `+=`和`*=` 来缩写这种表示。
281
281
282
282
```js run
283
283
let n = 2;
284
-
n += 5; // now n = 7 (same as n = n + 5)
285
-
n *= 2; // now n = 14 (same as n = n * 2)
284
+
n += 5; // 现在 n = 7(等同于 n = n + 5)
285
+
n *= 2; // 现在 n = 14(等同于 n = n * 2)
286
286
287
287
alert( n ); // 14
288
288
```
289
289
290
-
Short "modify-and-assign" operators exist for all arithmetical and bitwise operators: `/=`, `-=`, etc.
290
+
所有算术和位运算符都有简短的“修改并赋值”运算符:`/=` 和 `-=` 等。
291
291
292
-
Such operators have the same precedence as a normal assignment, so they run after most other calculations:
292
+
这类运算符的优先级与普通赋值运算符的优先级相同,所以它们在大多数其他运算之后执行:
293
293
294
294
```js run
295
295
let n = 2;
296
296
297
297
n *= 3 + 5;
298
298
299
-
alert( n ); // 16 (right part evaluated first, same as n *= 8)
299
+
alert( n ); // 16 (右边部分先被计算,等同于 n *= 8)
300
300
```
301
301
302
302
## 自增/自减
@@ -428,7 +428,7 @@ counter++;
428
428
- 右移 ( `>>` )
429
429
- 无符号右移 ( `>>>` )
430
430
431
-
These operators are used very rarely, when we need to fiddle with numbers on the very lowest (bitwise) level. We won't need these operators any time soon, as web development has little use of them, but in some special areas, such as cryptography, they are useful. You can read the [Bitwise Operators](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators) article on MDN when a need arises.
431
+
这些运算符很少被使用,一般是我们需要在最低级别(位)上操作数字时才使用。我们不会很快用到这些运算符,因为在 Web 开发中很少使用它们,但在某些特殊领域中,例如密码学,它们很有用。当你需要了解它们的时候,可以阅读 MDN 上的 [位操作符](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators) 一文。
0 commit comments