Skip to content

Translate some chapters to [Jul 17, 2020] part4 #801

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 7 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 6 additions & 6 deletions 1-js/05-data-types/10-destructuring-assignment/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,22 @@ for (let [key, value] of user) {
```
````

```smart header="Swap variables trick"
A well-known trick for swapping values of two variables:
```smart header="交换变量值的技巧"
一个用于交换变量值的知名技巧:

```js run
let guest = "Jane";
let admin = "Pete";

// Swap values: make guest=Pete, admin=Jane
// 交换值:让 guest=Pete, admin=Jane
[guest, admin] = [admin, guest];

alert(`${guest} ${admin}`); // Pete Jane (successfully swapped!)
alert(`${guest} ${admin}`); // Pete Jane(成功交换!)
```

Here we create a temporary array of two variables and immediately destructure it in swapped order.
这里我们创建了一个有两个变量的暂时数组,并且立即以与原先相反的顺序进行解构。

We can swap more than two variables this way.
我们可以用这种方式交换两个以上的变量。


### 剩余的 '...'
Expand Down
20 changes: 10 additions & 10 deletions 1-js/06-advanced-functions/04-var/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
2. `const`
3. `var`

The `var` declaration is similar to `let`. Most of the time we can replace `let` by `var` or vice-versa and expect things to work:
`var` 声明跟 `let` 相似。大部分情况下,我们可以用 `let` 代替 `var` 或者 `var` 代替 `let`,都能达到理想效果:

```js run
var message = "Hi";
alert(message); // Hi
```

But internally `var` is a very different beast, that originates from very old times. It's generally not used in modern scripts, but still lurks in the old ones.
但实际上,`var` 是一非常不同的野兽,但 `var` 却是一头完全不同的,源自非常古老的时代的怪兽。在现代脚本中一般不再使用它,但它仍然潜伏在旧脚本中。

If you don't plan on meeting such scripts you may even skip this chapter or postpone it.
如果你不打算接触这样的脚本,你甚至可以跳过本章或推迟阅读本章。

On the other hand, it's important to understand differences when migrating old scripts from `var` to `let`, to avoid odd errors.
另一方面,了解将旧脚本从 `var` 迁移到 `let` 时的区别,以避免奇怪的错误,是很重要的。

## "var" 没有块级作用域

Expand Down Expand Up @@ -80,32 +80,32 @@ function sayHi() {
}

sayHi();
alert(phrase); // Error: phrase is not defined (Check the Developer Console)
alert(phrase); // Error: phrase is not defined(检查开发者控制台)
```

可以看到,`var` 穿透了 `if`,`for` 和其它代码块。这是因为在早期的 JavaScript 中,块没有词法环境。而 `var` 就是这个时期的代表之一。

## "var" 允许重新声明

If we declare the same variable with `let` twice in the same scope, that's an error:
如果我们用 `let` 在同一作用域下将同一个声明两次,会出现错误:

```js run
let user;
let user; // SyntaxError: 'user' has already been declared
```

With `var`, we can redeclare a variable any number of times. If we use `var` with an already-declared variable, it's just ignored:
使用 `var`,我们可以重复声明一个变量,不管多少次都行。如果我们对一个已经声明的变量使用 `var`,此语句会被忽略:

```js run
var user = "Pete";

var user = "John"; // this "var" does nothing (already declared)
// ...it doesn't trigger an error
var user = "John"; // 这个 "var" 无效(因为变量已经声明过了)
// ……不会触发错误

alert(user); // John
```

## "var" variables can be declared below their use
## "var" 变量能够在其使用下声明

当函数开始的时候,就会处理 `var` 声明(脚本启动对应全局变量)。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ importance: 5

创建一个“节流”装饰者 `throttle(f, ms)` —— 返回一个包装器。

When it's called multiple times, it passes the call to `f` at maximum once per `ms` milliseconds.
当被多次调用,它会把调用传到 `f`,每 `ms` 毫秒最大一次。

The difference with debounce is that it's completely different decorator:
- `debounce` runs the function once after the "cooldown" period. Good for processing the final result.
- `throttle` runs it not more often than given `ms` time. Good for regular updates that shouldn't be very often.
与去抖的不同是,它是个完全不一样的装饰器:
- `debounce` 会在“冷却(cooldown)”期后调用一次。适合处理最终结果。
- `throttle` 调用频率不会大于所给的时间 `ms` 毫秒。适合用来定期刷新(启动)不应该高频执行的逻辑。

让我们看看现实生活中的应用程序,以便更好地理解这个需求,并了解它的来源。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ worker.slow = cachingDecorator(worker.slow);

对于许多实际应用,第三种方式就足够了,所以我们就用这个吧。

Also we need to pass not just `x`, but all arguments in `func.call`. Let's recall that in a `function()` we can get a pseudo-array of its arguments as `arguments`, so `func.call(this, x)` should be replaced with `func.call(this, ...arguments)`.
而且我们需要传入的不仅是 `x`,而是 `func.call` 所有的参数。让我们回想一下,`function()` 中我们可以得到一个包含所有参数的伪数组 `arguments`,那么 `func.call(this, x)` 应该被替换为 `func.call(this, ...arguments)`

这是一个更强大的 `cachingDecorator`:

Expand Down