Skip to content

feat: update translation to 2022-07-13 #1078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jul 13, 2022
Merged
6 changes: 3 additions & 3 deletions 1-js/01-getting-started/1-intro/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ JavaScript 是将这三件事结合在一起的唯一的浏览器技术。

此类语言的示例有:

- [CoffeeScript](http://coffeescript.org/) 是 JavaScript 的一种语法糖。它引入了更加简短的语法,使我们可以编写更清晰简洁的代码。通常,Ruby 开发者喜欢它。
- [TypeScript](http://www.typescriptlang.org/) 专注于添加“严格的数据类型”以简化开发,以更好地支持复杂系统的开发。由微软开发。
- [Flow](http://flow.org/) 也添加了数据类型,但是以一种不同的方式。由 Facebook 开发。
- [CoffeeScript](https://coffeescript.org/) 是 JavaScript 的一种语法糖。它引入了更加简短的语法,使我们可以编写更清晰简洁的代码。通常,Ruby 开发者喜欢它。
- [TypeScript](https://www.typescriptlang.org/) 专注于添加“严格的数据类型”以简化开发,以更好地支持复杂系统的开发。由微软开发。
- [Flow](https://flow.org/) 也添加了数据类型,但是以一种不同的方式。由 Facebook 开发。
- [Dart](https://www.dartlang.org/) 是一门独立的语言。它拥有自己的引擎,该引擎可以在非浏览器环境中运行(例如手机应用),它也可以被编译成 JavaScript。由 Google 开发。
- [Brython](https://brython.info/) 是一个 Python 到 JavaScript 的转译器,让我们可以在不使用 JavaScript 的情况下,以纯 Python 编写应用程序。
- [Kotlin](https://kotlinlang.org/docs/reference/js-overview.html) 是一个现代、简洁且安全的编程语言,编写出的应用程序可以在浏览器和 Node 环境中运行。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ JavaScript 是一门还在发展中的语言,定期会添加一些新的功能

要查看它们在基于浏览器的引擎及其他引擎中的支持情况,请看:

- <http://caniuse.com> —— 每个功能的支持表,例如,查看哪个引擎支持现代加密(cryptography)函数:<http://caniuse.com/#feat=cryptography>。
- <https://caniuse.com> —— 每个功能的支持表,例如,查看哪个引擎支持现代加密(cryptography)函数:<https://caniuse.com/#feat=cryptography>。
- <https://kangax.github.io/compat-table> —— 一份列有语言功能以及引擎是否支持这些功能的表格。

所有这些资源在实际开发中都有用武之地,因为它们包含了有关语言细节,以及它们被支持的程度等非常有价值的信息。
Expand Down
3 changes: 1 addition & 2 deletions 1-js/01-getting-started/3-code-editors/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ IDE 加载项目(通常包含很多文件),并且允许在不同文件之
如果你还没考虑好选哪一款 IDE,可以考虑下面两个:

- [Visual Studio Code](https://code.visualstudio.com/)(跨平台,免费)。
- [WebStorm](http://www.jetbrains.com/webstorm/)(跨平台,收费)。
- [WebStorm](https://www.jetbrains.com/webstorm/)(跨平台,收费)。

对于 Windows 系统来说,也有个叫 "Visual Studio" 的 IDE,请不要跟 "Visual Studio Code" 混淆。"Visual Studio" 是一个收费的、强大的 Windows 专用编辑器,它十分适合于 .NET 开发。用它进行 JavaScript 开发也不错。"Visual Studio" 有个免费的版本 [Visual Studio Community](https://www.visualstudio.com/vs/community/)。

Expand All @@ -31,7 +31,6 @@ IDE 加载项目(通常包含很多文件),并且允许在不同文件之

下面是一些值得你关注的“轻量编辑器”:

- [Atom](https://atom.io/)(跨平台,免费)。
- [Sublime Text](http://www.sublimetext.com)(跨平台,共享软件)。
- [Notepad++](https://notepad-plus-plus.org/)(Windows,免费)。
- [Vim](http://www.vim.org/) 和 [Emacs](https://www.gnu.org/software/emacs/) 也很棒,如果你知道怎么使用它们的话。
Expand Down
6 changes: 4 additions & 2 deletions 1-js/02-first-steps/04-variables/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ let message = 'Hello';
```

一些程序员采用下面的形式书写多个变量:

```js no-beautify
let user = 'John',
age = 25,
Expand Down Expand Up @@ -103,6 +104,7 @@ let user = 'John'
我们可以在盒子内放入任何值。

并且,这个盒子的值,我们想改变多少次,就可以改变多少次:

```js run
let message;

Expand Down Expand Up @@ -192,7 +194,7 @@ let my-name; // 连字符 '-' 不允许用于变量命名
```

```smart header="区分大小写"
命名为 `apple` 和 `AppLE` 的变量是不同的两个变量。
命名为 `apple` 和 `APPLE` 的变量是不同的两个变量。
```

````smart header="允许非英文字母,但不推荐"
Expand Down Expand Up @@ -260,7 +262,6 @@ myBirthday = '01.01.2001'; // 错误,不能对常量重新赋值

当程序员能确定这个变量永远不会改变的时候,就可以使用 `const` 来确保这种行为,并且清楚地向别人传递这一事实。


### 大写形式的常数

一个普遍的做法是将常量用作别名,以便记住那些在执行之前就已知的难以记住的值。
Expand Down Expand Up @@ -291,6 +292,7 @@ alert(color); // #FF7F00
作为一个“常数”,意味着值永远不变。但是有些常量在执行之前就已知了(比如红色的十六进制值),还有些在执行期间被“计算”出来,但初始赋值之后就不会改变。

例如:

```js
const pageLoadTime = /* 网页加载所需的时间 */;
```
Expand Down
37 changes: 25 additions & 12 deletions 1-js/02-first-steps/05-types/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,20 @@ n = 12.345;

## BigInt 类型 [#bigint-type]

在 JavaScript 中,"number" 类型无法表示大于 <code>(2<sup>53</sup>-1)</code>(即 `9007199254740991`),或小于 <code>-(2<sup>53</sup>-1)</code> 的整数。这是其内部表示形式导致的技术限制
在 JavaScript 中,"number" 类型无法安全地表示大于 <code>(2<sup>53</sup>-1)</code>(即 `9007199254740991`),或小于 <code>-(2<sup>53</sup>-1)</code> 的整数。

在大多数情况下,这个范围就足够了,但有时我们需要很大的数字,例如用于加密或微秒精度的时间戳。
更准确的说,"number" 类型可以存储更大的整数(最多 <code>1.7976931348623157 * 10<sup>308</sup></code>),但超出安全整数范围 <code>±(2<sup>53</sup>-1)</code> 会出现精度问题,因为并非所有数字都适合固定的 64 位存储。因此,可能存储的是“近似值”。

例如,这两个数字(正好超出了安全整数范围)是相同的:

```js
console.log(9007199254740991 + 1); // 9007199254740992
console.log(9007199254740991 + 2); // 9007199254740992
```

也就是说,所有大于 <code>(2<sup>53</sup>-1)</code> 的奇数都不能用 "number" 类型存储。

在大多数情况下,<code>±(2<sup>53</sup>-1)</code> 范围就足够了,但有时候我们需要整个范围非常大的整数,例如用于密码学或微秒精度的时间戳。

`BigInt` 类型是最近被添加到 JavaScript 语言中的,用于表示任意长度的整数。

Expand Down Expand Up @@ -261,16 +272,18 @@ typeof alert // "function" (3)

## 总结

JavaScript 中有八种基本的数据类型(译注:前七种为基本数据类型,也称为原始类型,而 `object` 为复杂数据类型)。

- `number` 用于任何类型的数字:整数或浮点数,在 <code>±(2<sup>53</sup>-1)</code> 范围内的整数。
- `bigint` 用于任意长度的整数。
- `string` 用于字符串:一个字符串可以包含 0 个或多个字符,所以没有单独的单字符类型。
- `boolean` 用于 `true` 和 `false`。
- `null` 用于未知的值 —— 只有一个 `null` 值的独立类型。
- `undefined` 用于未定义的值 —— 只有一个 `undefined` 值的独立类型。
- `symbol` 用于唯一的标识符。
- `object` 用于更复杂的数据结构。
JavaScript 中有八种基本的数据类型(译注:前七种为基本数据类型,也称为原始数据类型,而 `object` 为复杂数据类型)。

- 七种原始数据类型:
- `number` 用于任何类型的数字:整数或浮点数,在 <code>±(2<sup>53</sup>-1)</code> 范围内的整数。
- `bigint` 用于任意长度的整数。
- `string` 用于字符串:一个字符串可以包含 0 个或多个字符,所以没有单独的单字符类型。
- `boolean` 用于 `true` 和 `false`。
- `null` 用于未知的值 —— 只有一个 `null` 值的独立类型。
- `undefined` 用于未定义的值 —— 只有一个 `undefined` 值的独立类型。
- `symbol` 用于唯一的标识符。
- 以及一种非原始数据类型:
- `object` 用于更复杂的数据结构。

我们可以通过 `typeof` 运算符查看存储在变量中的数据类型。

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
答案:`null`,因为它是列表中第一个假值。

```js run
alert( 1 && null && 2 );
alert(1 && null && 2);
```

20 changes: 17 additions & 3 deletions 1-js/02-first-steps/13-while-for/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@

**循环** 是一种重复运行同一代码的方法。

```smart header="for..of 和 for..in 循环"
给进阶读者的一个小提示。

本文仅涵盖了基础的循环:`while`,`do..while` 和 `for(..; ..; ..)`。

如果你阅读本文是为了寻找其他类型的循环,那么:

- 用于遍历对象属性的 `for..in` 循环请见:[for..in](info:object#forin)。
- 用于遍历数组和可迭代对象的循环分别请见:[for..of](info:array#loops) 和 [iterables](info:iterable)。

否则,请继续阅读。
```

## "while" 循环

`while` 循环的语法如下:
Expand Down Expand Up @@ -162,10 +175,8 @@ for (i = 0; i < 3; i++) { // 使用现有的变量

alert(i); //3,可见,因为是在循环之外声明的
```

````


### 省略语句段

`for` 循环的任何语句段都可以被省略。
Expand Down Expand Up @@ -286,7 +297,6 @@ if (i > 5) {

……用问号重写:


```js no-beautify
(i > 5) ? alert(i) : *!*continue*/!*; // continue 不允许在这个位置
```
Expand Down Expand Up @@ -321,6 +331,7 @@ alert('Done!');
在 `input` 之后的普通 `break` 只会打破内部循环。这还不够 —— 标签可以实现这一功能!

**标签** 是在循环之前带有冒号的标识符:

```js
labelName: for (...) {
...
Expand All @@ -342,6 +353,7 @@ labelName: for (...) {
// 用得到的值做些事……
}
}

alert('Done!');
```

Expand All @@ -362,13 +374,15 @@ for (let i = 0; i < 3; i++) { ... }
标签不允许我们跳到代码的任意位置。

例如,这样做是不可能的:

```js
break label; // 跳转至下面的 label 处(无效)

label: for (...)
```

`break` 指令必须在代码块内。从技术上讲,任何被标记的代码块都有效,例如:

```js
label: {
// ...
Expand Down
4 changes: 2 additions & 2 deletions 1-js/02-first-steps/16-function-expressions/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ ask("Do you agree?", showOk, showCancel);

主要思想是我们传递一个函数,并期望在稍后必要时将其“回调”。在我们的例子中,`showOk` 是回答 "yes" 的回调,`showCancel` 是回答 "no" 的回调。

我们可以用函数表达式对同样的函数进行大幅简写
我们可以使用函数表达式来编写一个等价的、更简洁的函数

```js run no-beautify
function ask(question, yes, no) {
Expand Down Expand Up @@ -186,7 +186,7 @@ ask(

首先是语法:如何通过代码对它们进行区分。

- **函数声明**:在主代码流中声明为单独的语句的函数
- **函数声明**:在主代码流中声明为单独的语句的函数

```js
// 函数声明
Expand Down
4 changes: 2 additions & 2 deletions 1-js/02-first-steps/18-javascript-specials/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ for(;;) {

该指令必须位于 JavaScript 脚本的顶部或函数体的开头。

如果没有 `"use strict"`,所有东西仍可以正常工作,但是某些特性的表现方式与旧式「兼容」方式相同。我们通常更喜欢现代的方式。
如果没有 `"use strict"`,所有东西仍可以正常工作,但某些功能将以老式的“兼容”方式运行。我们通常更喜欢现代的方式。

语言的一些现代特征(比如我们将来要学习的类)会隐式地启用严格模式。

Expand Down Expand Up @@ -144,7 +144,7 @@ JavaScript 支持以下运算符:
: 简单的赋值:`a = b` 和合并了其他操作的赋值:`a * = 2`。

按位运算符
: 按位运算符在最低位级上操作 32 位的整数:详见 [文档](mdn:/JavaScript/Guide/Expressions_and_Operators#Bitwise)。
: 按位运算符在最低位级上操作 32 位的整数:详见 [文档](mdn:/JavaScript/Guide/Expressions_and_Operators#bitwise_operators)。

三元运算符
: 唯一具有三个参数的操作:`cond ? resultA : resultB`。如果 `cond` 为真,则返回 `resultA`,否则返回 `resultB`。
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion 1-js/03-code-quality/01-debugging-chrome/chrome-tabs.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion 1-js/03-code-quality/03-comments/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function pow(x, n) {

顺便说一句,很多诸如 [WebStorm](https://www.jetbrains.com/webstorm/) 这样的编辑器,都可以很好地理解和使用这些注释,来提供自动补全和一些自动化代码检查工作。

当然,也有一些像 [JSDoc 3](https://github.com/jsdoc3/jsdoc) 这样的工具,可以通过注释直接生成 HTML 文档。你可以在 <http://usejsdoc.org/> 阅读更多关于 JSDoc 的信息。
当然,也有一些像 [JSDoc 3](https://github.com/jsdoc/jsdoc) 这样的工具,可以通过注释直接生成 HTML 文档。你可以在 <https://jsdoc.app> 阅读更多关于 JSDoc 的信息。

为什么任务以这种方式解决?
: 写了什么代码很重要。但是为什么 **不** 那样写可能对于理解正在发生什么更重要。为什么任务是通过这种方式解决的?代码并没有给出答案。
Expand Down
2 changes: 1 addition & 1 deletion 1-js/03-code-quality/06-polyfills/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ if (!Math.trunc) { // 如果没有这个函数
}
```

JavaScript 是一种高度动态的语言脚本可以添加/修改任何函数,甚至包括内建函数。
JavaScript 是一种高度动态的语言脚本可以添加/修改任何函数,甚至包括内建函数。

两个有趣的 polyfill 库:
- [core js](https://github.com/zloirock/core-js) 支持了很多特性,允许只包含需要的特性。
Expand Down
2 changes: 1 addition & 1 deletion 1-js/04-object-basics/01-object/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ alert( "test" in obj ); // true,属性存在!
这种情况很少发生,因为通常情况下不应该给对象赋值 `undefined`。我们通常会用 `null` 来表示未知的或者空的值。因此,`in` 运算符是代码中的特殊来宾。


## "for..in" 循环
## "for..in" 循环 [#forin]

为了遍历一个对象的所有键(key),可以使用一个特殊形式的循环:`for..in`。这跟我们在前面学到的 `for(;;)` 循环是完全不一样的东西。

Expand Down
14 changes: 5 additions & 9 deletions 1-js/04-object-basics/02-object-copy/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,9 @@ alert( a == b ); // false

那么,拷贝一个对象变量会又创建一个对相同对象的引用。

但是,如果我们想要复制一个对象,那该怎么做呢?创建一个独立的拷贝,克隆?
但是,如果我们想要复制一个对象,那该怎么做呢?

这也是可行的,但稍微有点困难,因为 JavaScript 没有提供对此操作的内建的方法。但很少需要这样做 —— 通过引用进行拷贝在大多数情况下已经满足了。

但是,如果我们真的想要这样做,那么就需要创建一个新对象,并通过遍历现有属性的结构,在原始类型值的层面,将其复制到新对象,以复制已有对象的结构。
我们可以创建一个新对象,通过遍历已有对象的属性,并在原始类型值的层面复制它们,以实现对已有对象结构的复制。

就像这样:

Expand Down Expand Up @@ -190,7 +188,7 @@ let clone = Object.assign({}, user);

## 深层克隆

到现在为止,我们都假设 `user` 的所有属性均为原始类型。但属性可以是对其他对象的引用。那应该怎样处理它们呢?
到现在为止,我们都假设 `user` 的所有属性均为原始类型。但属性可以是对其他对象的引用。

例如:
```js run
Expand All @@ -207,8 +205,6 @@ alert( user.sizes.height ); // 182

现在这样拷贝 `clone.sizes = user.sizes` 已经不足够了,因为 `user.sizes` 是个对象,它会以引用形式被拷贝。因此 `clone` 和 `user` 会共用一个 sizes:

就像这样:

```js run
let user = {
name: "John",
Expand All @@ -224,10 +220,10 @@ alert( user.sizes === clone.sizes ); // true,同一个对象

// user 和 clone 分享同一个 sizes
user.sizes.width++; // 通过其中一个改变属性值
alert(clone.sizes.width); // 51,能从另外一个看到变更的结果
alert(clone.sizes.width); // 51,能从另外一个获取到变更后的结果
```

为了解决这个问题,我们应该使用一个拷贝循环来检查 `user[key]` 的每个值,如果它是一个对象,那么也复制它的结构。这就是所谓的“深拷贝”。
为了解决这个问题,并让 `user` 和 `clone` 成为两个真正独立的对象,我们应该使用一个拷贝循环来检查 `user[key]` 的每个值,如果它是一个对象,那么也复制它的结构。这就是所谓的“深拷贝”。

我们可以使用递归来实现它。或者为了不重复造轮子,采用现有的实现,例如 [lodash](https://lodash.com) 库的 [_.cloneDeep(obj)](https://lodash.com/docs#cloneDeep)。

Expand Down
Loading