Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions 1-js/02-first-steps/02-structure/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ alert("There will be an error")
现在,如果我们运行代码,只有第一个 `alert` 语句的内容被显示了出来,随后我们收到了一个错误!

但是,如果我们在第一个 `alert` 语句末尾加上一个分号,就工作正常了:

```js run
alert("All fine now");

Expand Down Expand Up @@ -137,7 +136,7 @@ alert('World');
```

```smart header="使用热键!"
在大多数的编辑器中,一行代码可以使用 `key:Ctrl+/` 热键进行单行注释,诸如 `key:Ctrl+Shift+/` 的热键可以进行多行注释(选择代码,然后按下热键)。对于 Mac 电脑,应使用 `key:Cmd` 而不是 `key:Ctrl`。
在大多数的编辑器中,一行代码可以使用 `key:Ctrl+/` 热键进行单行注释,诸如 `key:Ctrl+Shift+/` 的热键可以进行多行注释(选择代码,然后按下热键)。对于 Mac 电脑,应使用 `key:Cmd` 而不是 `key:Ctrl`,使用 `key:Option` 而不是 `key:Shift`
```

````warn header="不支持注释嵌套!"
Expand Down
2 changes: 1 addition & 1 deletion 1-js/02-first-steps/03-strict-mode/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ alert("some code");
```warn header="没有办法取消 `use strict`"
没有类似于 `"no use strict"` 这样的指令可以使程序返回默认模式。

一旦进入了严格模式,就没有退路了
一旦进入了严格模式,就没有回头路了
```

## 浏览器控制台
Expand Down
2 changes: 1 addition & 1 deletion 1-js/02-first-steps/05-types/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ alert( "the result is ${1 + 2}" ); // the result is ${1 + 2}(使用双引号
我们会在 <info:string> 一节中学习字符串的更多细节。

```smart header="JavaScript 中没有 *character* 类型。"
在一些语言中,单个字符有一个特殊的 character 类型,在 C 语言和 Java 语言中被称为 "char"。
在一些语言中,单个字符有一个特殊的 "character" 类型,在 C 语言和 Java 语言中被称为 "char"。

在 JavaScript 中没有这种类型。只有一种 `string` 类型,一个字符串可以包含一个或多个字符。
```
Expand Down
17 changes: 2 additions & 15 deletions 1-js/02-first-steps/06-type-conversions/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ number 类型转换规则:
|<code>true&nbsp;和&nbsp;false</code> | `1` and `0` |
| `string` | 去掉首尾空格后的纯数字字符串中含有的数字。如果剩余字符串为空,则转换结果为 `0`。否则,将会从剩余字符串中“读取”数字。当类型转换出现 error 时返回 `NaN`。 |

> 译注:字符串转换为 number 类型时,除了 `undefined`、`null` 和 `boolean` 三种特殊情况,只有字符串是由空格和数字组成时,才能转换成功,否则会出现 error 返回 `NaN`。

例子:

```js run
Expand All @@ -81,20 +79,9 @@ alert( Number(true) ); // 1
alert( Number(false) ); // 0
```

请注意 `null` 和 `undefined` 在这有点不同:`null` 变成数字 `0`,`undefined` 变成 `NaN`。

````smart header="加号 '+' 连接字符串"
几乎所有的算术运算符都将值转换为数字进行运算,加号 `+` 运算符是个例外。如果其中一个运算元是字符串,则另一个也会被转换为字符串。
请注意 `null` 和 `undefined` 在这有点不同:`null` 变成数字 `0`,`undefined` 变成 `NaN`。(译注:此外,字符串转换为 number 类型时,除了 `undefined`、`null` 和 `boolean` 三种特殊情况,只有字符串是由空格和数字组成时,才能转换成功,否则会出现 error 返回 `NaN`。)

然后,将两者连接在一起:

```js run
alert( 1 + '2' ); // '12' (字符串在加号右边)
alert( '1' + 2 ); // '12' (字符串在加号左边)
```

这仅仅发生在至少其中一方为字符串的情况下。否则值会被转换为数字。
````
大多数数学运算符也执行这种转换,我们将在下一节中进行介绍。

## 布尔型转换

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ undefined + 1 = NaN // (6)
```

1. 有字符串的加法 `"" + 1`,首先会将数字 `1` 转换为一个字符串:`"" + 1 = "1"`,然后我们得到 `"1" + 0`,再次应用同样的规则得到最终的结果。
2. 减法 `-`(就像大多数数学操作那样)只能用于数字,它会使空字符串 `""` 转换为 `0`。
2. 减法 `-`(像大多数数学运算一样)只能用于数字,它会使空字符串 `""` 转换为 `0`。
3. 带字符串的加法会将数字 `5` 加到字符串之后。
4. 减法始终将字符串转换为数字,因此它会使 `" -9 "` 转换为数字 `-9`(忽略了字符串首尾的空格)。
5. `null` 经过数字转换之后会变为 `0`。
6. `undefined` 经过数字转换之后会变为 `NaN`。
7. 字符串转换为数字时,会忽略字符串的开头和结尾处的空格字符。在这里,整个字符串由空格字符组成,包括 `\t`、`\n` 以及它们之间的“常规”空格。因此,类似于空字符串,所以会变为 `0`。
7. 字符串转换为数字时,会忽略字符串的首尾处的空格字符。在这里,整个字符串由空格字符组成,包括 `\t`、`\n` 以及它们之间的“常规”空格。因此,类似于空字符串,所以会变为 `0`。
8 changes: 4 additions & 4 deletions 1-js/02-first-steps/07-operators/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ alert( +apples + +oranges ); // 5
| 优先级 | 名称 | 符号 |
|------------|------|------|
| ... | ... | ... |
| 16 | 一元加号 | `+` |
| 16 | 一元负号 | `-` |
| 14 | 乘号 | `*` |
| 14 | 除号 | `/` |
| 17 | 一元加号 | `+` |
| 17 | 一元负号 | `-` |
| 15 | 乘号 | `*` |
| 15 | 除号 | `/` |
| 13 | 加号 | `+` |
| 13 | 减号 | `-` |
| ... | ... | ... |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ null === +"\n0\n" → false
结果的原因:

1. 数字间比较大小,显然得 true。
2. 按词典顺序比较,得 false。
2. 按词典顺序比较,得 false。`"a"` 比 `"p"` 小。
3. 与第 2 题同理,首位字符 `"2"` 大于 `"1"`。
4. `null` 只与 `undefined` 互等。
5. 严格相等模式下,类型不同得 false。
Expand Down
6 changes: 3 additions & 3 deletions 1-js/04-object-basics/01-object/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ alert(obj.__proto__); // [object Object] — 值为对象,与预期结果不

因为用户可能会选择 `__proto__` 作为键,这个赋值的逻辑就失败了(像上面那样)。

稍后我们将看到该问题的解决方法
1. 我们将在 [](info:prototype-methods) 一章中了解,如何使对象将 `__proto__` 作为常规属性对待
2. 在 <info:map-set> 一章中还将研究另一种数据结构 [Map](info:map-set),该结构支持任意键
有两个解决该问题的方法
1. 修改对象的行为,使其将 `__proto__` 作为常规属性对待。我们将在 [](info:prototype-methods) 一章中学习如何进行修改
2. 使用支持任意键的数据结构 [Map](info:map-set)。我们将在 <info:map-set> 章节学习它

## 属性存在性测试,"in" 操作符

Expand Down
16 changes: 0 additions & 16 deletions 1-js/04-object-basics/03-symbol/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,22 +178,6 @@ alert( clone[id] ); // 123

这里并不矛盾,就是这样设计的。这里的想法是当我们克隆或者合并一个 object 时,通常希望 **所有** 属性被复制(包括像 `id` 这样的 Symbol)。

````smart header="其他类型的属性键被强制为字符串"
我们只能在对象中使用字符串或 symbol 作为键,其它类型会被转换为字符串。

例如,在作为属性键使用时,数字 `0` 变成了字符串 `"0"`:

```js run
let obj = {
0: "test" // 和 "0": "test" 一样
};

// 两个 alert 都访问相同的属性(Number 0 被转换为字符串 "0")
alert( obj["0"] ); // test
alert( obj[0] ); // test(同一个属性)
```
````

## 全局 symbol

正如我们所看到的,通常所有的 Symbol 都是不同的,即使它们有相同的名字。但有时我们想要名字相同的 Symbol 具有相同的实体。例如,应用程序的不同部分想要访问的 Symbol `"id"` 指的是完全相同的属性。
Expand Down
2 changes: 1 addition & 1 deletion 1-js/05-data-types/03-string/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ alert( str );

因此浏览器需要知道要比较的语言。

幸运的是,所有现代浏览器(IE10- 需要额外的库 [Intl.JS](https://github.com/andyearnshaw/Intl.js/)) 都支持国际化标准 [ECMA 402](http://www.ecma-international.org/ecma-402/1.0/ECMA-402.pdf)。
幸运的是,所有现代浏览器(IE10- 需要额外的库 [Intl.JS](https://github.com/andyearnshaw/Intl.js/)) 都支持国际化标准 [ECMA-402](http://www.ecma-international.org/ecma-402/1.0/ECMA-402.pdf)。

它提供了一种特殊的方法来比较不同语言的字符串,遵循它们的规则。

Expand Down
12 changes: 6 additions & 6 deletions 1-js/05-data-types/04-array/10-maximal-subarray/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ importance: 2
例如:

```js
getMaxSubSum([-1, *!*2, 3*/!*, -9]) = 5(高亮项的加和)
getMaxSubSum([*!*2, -1, 2, 3*/!*, -9]) = 6
getMaxSubSum([-1, 2, 3, -9, *!*11*/!*]) = 11
getMaxSubSum([-2, -1, *!*1, 2*/!*]) = 3
getMaxSubSum([*!*100*/!*, -9, 2, -3, 5]) = 100
getMaxSubSum([*!*1, 2, 3*/!*]) = 6(所有项的和)
getMaxSubSum([-1, *!*2, 3*/!*, -9]) == 5(高亮项的加和)
getMaxSubSum([*!*2, -1, 2, 3*/!*, -9]) == 6
getMaxSubSum([-1, 2, 3, -9, *!*11*/!*]) == 11
getMaxSubSum([-2, -1, *!*1, 2*/!*]) == 3
getMaxSubSum([*!*100*/!*, -9, 2, -3, 5]) == 100
getMaxSubSum([*!*1, 2, 3*/!*]) == 6(所有项的和)
```

如果所有项都是负数,那就一个项也不取(子数组是空的),所以返回的是 0:
Expand Down
70 changes: 1 addition & 69 deletions 1-js/05-data-types/04-array/array-pop.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading