mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-24 10:22:23 +00:00
docs: fix wangdoc-es6 #4
This commit is contained in:
parent
2ed3a6ba3b
commit
04dddb2d7c
@ -1141,14 +1141,14 @@ Atomics.add(ia, 112, 1); // 正确
|
|||||||
|
|
||||||
`store()`方法用来向共享内存写入数据,`load()`方法用来从共享内存读出数据。比起直接的读写操作,它们的好处是保证了读写操作的原子性。
|
`store()`方法用来向共享内存写入数据,`load()`方法用来从共享内存读出数据。比起直接的读写操作,它们的好处是保证了读写操作的原子性。
|
||||||
|
|
||||||
此外,它们还用来解决一个问题:多个线程使用共享内存的某个位置作为开关(flag),一旦该位置的值变了,就执行特定操作。这时,必须保证该位置的赋值操作,一定是在它前面的所有可能会改写内存的操作结束后执行;而该位置的取值操作,一定是在它后面所有可能会读取该位置的操作开始之前执行。`store`方法和`load`方法就能做到这一点,编译器不会为了优化,而打乱机器指令的执行顺序。
|
此外,它们还用来解决一个问题:多个线程使用共享内存的某个位置作为开关(flag),一旦该位置的值变了,就执行特定操作。这时,必须保证该位置的赋值操作,一定是在它前面的所有可能会改写内存的操作结束后执行;而该位置的取值操作,一定是在它后面所有可能会读取该位置的操作开始之前执行。`store()`方法和`load()`方法就能做到这一点,编译器不会为了优化,而打乱机器指令的执行顺序。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
Atomics.load(array, index)
|
Atomics.load(typedArray, index)
|
||||||
Atomics.store(array, index, value)
|
Atomics.store(typedArray, index, value)
|
||||||
```
|
```
|
||||||
|
|
||||||
`store`方法接受三个参数:SharedBuffer 的视图、位置索引和值,返回`sharedArray[index]`的值。`load`方法只接受两个参数:SharedBuffer 的视图和位置索引,也是返回`sharedArray[index]`的值。
|
`store`方法接受三个参数:SharedArrayBuffer 的视图、位置索引和值,返回`sharedArrayBuffer[index]`的值。`load`方法只接受两个参数:SharedArrayBuffer 的视图和位置索引,也是返回`sharedArrayBuffer[index]`的值。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
// 主线程 main.js
|
// 主线程 main.js
|
||||||
|
@ -63,9 +63,9 @@ function idMaker() {
|
|||||||
|
|
||||||
const it = idMaker();
|
const it = idMaker();
|
||||||
|
|
||||||
|
it.next().value.then(o => console.log(o)) // 0
|
||||||
it.next().value.then(o => console.log(o)) // 1
|
it.next().value.then(o => console.log(o)) // 1
|
||||||
it.next().value.then(o => console.log(o)) // 2
|
it.next().value.then(o => console.log(o)) // 2
|
||||||
it.next().value.then(o => console.log(o)) // 3
|
|
||||||
// ...
|
// ...
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -752,7 +752,7 @@ export { output };
|
|||||||
```javascript
|
```javascript
|
||||||
// awaiting.js
|
// awaiting.js
|
||||||
let output;
|
let output;
|
||||||
(async function1 main() {
|
(async function main() {
|
||||||
const dynamic = await import(someMission);
|
const dynamic = await import(someMission);
|
||||||
const data = await fetch(url);
|
const data = await fetch(url);
|
||||||
output = someProcess(dynamic.default, data);
|
output = someProcess(dynamic.default, data);
|
||||||
|
@ -735,9 +735,9 @@ class Widget {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
上面代码中,`_bar`方法前面的下划线,表示这是一个只限于内部使用的私有方法。但是,这种命名是不保险的,在类的外部,还是可以调用到这个方法。
|
上面代码中,`_bar()`方法前面的下划线,表示这是一个只限于内部使用的私有方法。但是,这种命名是不保险的,在类的外部,还是可以调用到这个方法。
|
||||||
|
|
||||||
另一种方法就是索性将私有方法移出模块,因为模块内部的所有方法都是对外可见的。
|
另一种方法就是索性将私有方法移出类,因为类内部的所有方法都是对外可见的。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
class Widget {
|
class Widget {
|
||||||
@ -753,7 +753,7 @@ function bar(baz) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
上面代码中,`foo`是公开方法,内部调用了`bar.call(this, baz)`。这使得`bar`实际上成为了当前模块的私有方法。
|
上面代码中,`foo`是公开方法,内部调用了`bar.call(this, baz)`。这使得`bar()`实际上成为了当前类的私有方法。
|
||||||
|
|
||||||
还有一种方法是利用`Symbol`值的唯一性,将私有方法的名字命名为一个`Symbol`值。
|
还有一种方法是利用`Symbol`值的唯一性,将私有方法的名字命名为一个`Symbol`值。
|
||||||
|
|
||||||
|
@ -286,7 +286,7 @@ import { bar } from 'my_module';
|
|||||||
import { foo, bar } from 'my_module';
|
import { foo, bar } from 'my_module';
|
||||||
```
|
```
|
||||||
|
|
||||||
上面代码中,虽然`foo`和`bar`在两个语句中加载,但是它们对应的是同一个`my_module`实例。也就是说,`import`语句是 Singleton 模式。
|
上面代码中,虽然`foo`和`bar`在两个语句中加载,但是它们对应的是同一个`my_module`模块。也就是说,`import`语句是 Singleton 模式。
|
||||||
|
|
||||||
目前阶段,通过 Babel 转码,CommonJS 模块的`require`命令和 ES6 模块的`import`命令,可以写在同一个模块里面,但是最好不要这样做。因为`import`在静态解析阶段执行,所以它是一个模块之中最早执行的。下面的代码可能不会得到预期结果。
|
目前阶段,通过 Babel 转码,CommonJS 模块的`require`命令和 ES6 模块的`import`命令,可以写在同一个模块里面,但是最好不要这样做。因为`import`在静态解析阶段执行,所以它是一个模块之中最早执行的。下面的代码可能不会得到预期结果。
|
||||||
|
|
||||||
|
@ -497,7 +497,7 @@ $ npm i -g eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react
|
|||||||
`index.js`文件的代码如下。
|
`index.js`文件的代码如下。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var unusued = 'I have no purpose!';
|
var unused = 'I have no purpose!';
|
||||||
|
|
||||||
function greet() {
|
function greet() {
|
||||||
var message = 'Hello, World!';
|
var message = 'Hello, World!';
|
||||||
@ -513,7 +513,7 @@ greet();
|
|||||||
$ eslint index.js
|
$ eslint index.js
|
||||||
index.js
|
index.js
|
||||||
1:1 error Unexpected var, use let or const instead no-var
|
1:1 error Unexpected var, use let or const instead no-var
|
||||||
1:5 error unusued is defined but never used no-unused-vars
|
1:5 error unused is defined but never used no-unused-vars
|
||||||
4:5 error Expected indentation of 2 characters but found 4 indent
|
4:5 error Expected indentation of 2 characters but found 4 indent
|
||||||
4:5 error Unexpected var, use let or const instead no-var
|
4:5 error Unexpected var, use let or const instead no-var
|
||||||
5:5 error Expected indentation of 2 characters but found 4 indent
|
5:5 error Expected indentation of 2 characters but found 4 indent
|
||||||
@ -522,3 +522,4 @@ index.js
|
|||||||
```
|
```
|
||||||
|
|
||||||
上面代码说明,原文件有五个错误,其中两个是不应该使用`var`命令,而要使用`let`或`const`;一个是定义了变量,却没有使用;另外两个是行首缩进为 4 个空格,而不是规定的 2 个空格。
|
上面代码说明,原文件有五个错误,其中两个是不应该使用`var`命令,而要使用`let`或`const`;一个是定义了变量,却没有使用;另外两个是行首缩进为 4 个空格,而不是规定的 2 个空格。
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user