Skip to content

Commit

Permalink
zh-cn: Format /web/javascript using Prettier (part 2) (#14698)
Browse files Browse the repository at this point in the history
Co-authored-by: Allo <[email protected]>
  • Loading branch information
queengooborg and yin1999 authored Jul 29, 2023
1 parent 665ae15 commit 320ff75
Show file tree
Hide file tree
Showing 100 changed files with 1,007 additions and 846 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ ArrayBuffer[Symbol.species]; // function ArrayBuffer()
```js
class MyArrayBuffer extends ArrayBuffer {
// Overwrite MyArrayBuffer species to the parent ArrayBuffer constructor
static get [Symbol.species]() { return ArrayBuffer; }
static get [Symbol.species]() {
return ArrayBuffer;
}
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ slug: Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView

## 语法

```js
```js-nolint
ArrayBuffer.isView(value)
```

Expand All @@ -30,15 +30,15 @@ ArrayBuffer.isView(value)
## 示例

```js
ArrayBuffer.isView(); // false
ArrayBuffer.isView([]); // false
ArrayBuffer.isView({}); // false
ArrayBuffer.isView(null); // false
ArrayBuffer.isView(undefined); // false
ArrayBuffer.isView(); // false
ArrayBuffer.isView([]); // false
ArrayBuffer.isView({}); // false
ArrayBuffer.isView(null); // false
ArrayBuffer.isView(undefined); // false
ArrayBuffer.isView(new ArrayBuffer(10)); // false

ArrayBuffer.isView(new Uint8Array()); // true
ArrayBuffer.isView(new Float32Array()); // true
ArrayBuffer.isView(new Uint8Array()); // true
ArrayBuffer.isView(new Float32Array()); // true
ArrayBuffer.isView(new Int8Array(10).subarray(0, 3)); // true

var buffer = new ArrayBuffer(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ async function* createAsyncGenerator() {
yield await Promise.resolve(3);
}
const asyncGen = createAsyncGenerator();
asyncGen.next()
.then((res) => console.log(res.value)); // 1
asyncGen.next()
.then((res) => console.log(res.value)); // 2
asyncGen.next()
.then((res) => console.log(res.value)); // 3
asyncGen.next().then((res) => console.log(res.value)); // 1
asyncGen.next().then((res) => console.log(res.value)); // 2
asyncGen.next().then((res) => console.log(res.value)); // 3
```

## 实例属性
Expand Down Expand Up @@ -74,17 +71,16 @@ async function* generate() {
yield delayedValue(250, 4);
yield delayedValue(125, 5);
yield delayedValue(50, 6);
console.log('All done!');
console.log("All done!");
}

async function main() {
for await (const value of generate()) {
console.log('value', value);
console.log("value", value);
}
}

main()
.catch((e) => console.error(e));
main().catch((e) => console.error(e));
```

## 规范
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var ta = new Uint8Array(sab);
ta[0] = 5;

Atomics.and(ta, 0, 1); // returns 0, the old value
Atomics.load(ta, 0); // 1
Atomics.load(ta, 0); // 1
```

## 规范
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var ta = new Uint8Array(sab);
ta[0] = 2;

Atomics.or(ta, 0, 1); // returns 2, the old value
Atomics.load(ta, 0); // 3
Atomics.load(ta, 0); // 3
```

## 规范
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const ta = new Uint8Array(sab);
ta[0] = 5;

Atomics.xor(ta, 0, 1); // returns 5, the old value
Atomics.load(ta, 0); // 4
Atomics.load(ta, 0); // 4
```

## 规范
Expand Down
48 changes: 26 additions & 22 deletions files/zh-cn/web/javascript/reference/global_objects/bigint/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const hugeString = BigInt("9007199254740991");
const hugeHex = BigInt("0x1fffffffffffff");
// ↪ 9007199254740991n

const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111");
const hugeBin = BigInt(
"0b11111111111111111111111111111111111111111111111111111",
);
// ↪ 9007199254740991n
```

Expand All @@ -34,14 +36,14 @@ const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111"
使用 `typeof` 测试时, `BigInt` 对象返回 "bigint" :

```js
typeof 1n === 'bigint'; // true
typeof BigInt('1') === 'bigint'; // true
typeof 1n === "bigint"; // true
typeof BigInt("1") === "bigint"; // true
```

使用 `Object` 包装后, `BigInt` 被认为是一个普通 "object" :

```js
typeof Object(1n) === 'object'; // true
typeof Object(1n) === "object"; // true
```

### 运算
Expand Down Expand Up @@ -91,29 +93,29 @@ const rounded = 5n / 2n;
`BigInt` 和 {{jsxref("Global_Objects/Number", "Number")}} 不是严格相等的,但是宽松相等的。

```js
0n === 0
0n === 0;
// ↪ false

0n == 0
0n == 0;
// ↪ true
```

{{jsxref("Global_Objects/Number", "Number")}} 和 `BigInt` 可以进行比较。

```js
1n < 2
1n < 2;
// ↪ true

2n > 1
2n > 1;
// ↪ true

2 > 2
2 > 2;
// ↪ false

2n > 2
2n > 2;
// ↪ false

2n >= 2
2n >= 2;
// ↪ true
```

Expand All @@ -134,7 +136,7 @@ mixed.sort();
Object(0n) === Object(0n); // false

const o = Object(0n);
o === o // true
o === o; // true
```

### 条件
Expand All @@ -143,29 +145,29 @@ o === o // true

```js
if (0n) {
console.log('Hello from the if!');
console.log("Hello from the if!");
} else {
console.log('Hello from the else!');
console.log("Hello from the else!");
}

// ↪ "Hello from the else!"

0n || 12n
0n || 12n;
// ↪ 12n

0n && 12n
0n && 12n;
// ↪ 0n

Boolean(0n)
Boolean(0n);
// ↪ false

Boolean(12n)
Boolean(12n);
// ↪ true

!12n
!12n;
// ↪ false

!0n
!0n;
// ↪ true
```

Expand Down Expand Up @@ -205,7 +207,9 @@ Boolean(12n)
对任何 `BigInt` 值使用 {{jsxref("JSON.stringify()")}} 都会引发 `TypeError`,因为默认情况下 `BigInt` 值不会在 `JSON` 中序列化。但是,如果需要,可以实现 `toJSON` 方法:

```js
BigInt.prototype.toJSON = function() { return this.toString(); }
BigInt.prototype.toJSON = function () {
return this.toString();
};
```

`JSON.stringify` 现在生成如下字符串,而不是抛出异常:
Expand Down Expand Up @@ -243,7 +247,7 @@ function nthPrime(nth) {
return prime;
}

nthPrime(20n)
nthPrime(20n);
// ↪ 73n
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,24 @@ console.log(bigint.toLocaleString());
const bigint = 123456789123456789n;

// 德国使用句号表示千位
console.log(bigint.toLocaleString('de-DE'));
console.log(bigint.toLocaleString("de-DE"));
// 123.456.789.123.456.789

// 大多数说阿拉伯语的阿拉伯国家使用东阿拉伯数字
console.log(bigint.toLocaleString('ar-EG'));
console.log(bigint.toLocaleString("ar-EG"));
// ١٢٣٬٤٥٦٬٧٨٩٬١٢٣٬٤٥٦٬٧٨٩

// 印度使用千、万、千万分隔符
console.log(bigint.toLocaleString('en-IN'));
console.log(bigint.toLocaleString("en-IN"));
// 1,23,45,67,89,12,34,56,789

// nu 扩展键要求使用编号系统,例如中文十进制数
console.log(bigint.toLocaleString('zh-Hans-CN-u-nu-hanidec'));
console.log(bigint.toLocaleString("zh-Hans-CN-u-nu-hanidec"));
// 一二三,四五六,七八九,一二三,四五六,七八九

// 当使用的语言不被支持,例如
// 巴厘岛语,则可以包含一种回退语言,这里以印尼语为例
console.log(bigint.toLocaleString(['ban', 'id']));
console.log(bigint.toLocaleString(["ban", "id"]));
// 123.456.789.123.456.789
```

Expand All @@ -97,15 +97,19 @@ console.log(bigint.toLocaleString(['ban', 'id']));
const bigint = 123456789123456789n;

// 要求使用货币格式
console.log(bigint.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }));
console.log(
bigint.toLocaleString("de-DE", { style: "currency", currency: "EUR" }),
);
// 123.456.789.123.456.789,00 €

// 日元不使用小数单位
console.log(bigint.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' }))
console.log(
bigint.toLocaleString("ja-JP", { style: "currency", currency: "JPY" }),
);
// ¥123,456,789,123,456,789

// 保留三位有效数字
console.log(bigint.toLocaleString('en-IN', { maximumSignificantDigits: 3 }));
console.log(bigint.toLocaleString("en-IN", { maximumSignificantDigits: 3 }));
// 1,23,00,00,00,00,00,00,000
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ bigIntObj.toString([radix])
### Using `toString`

```js
17n.toString(); // '17'
66n.toString(2); // '1000010'
254n.toString(16); // 'fe'
-10n.toString(2); // -1010'
-0xffn.toString(2); // '-11111111'
17n.toString(); // '17'
66n.toString(2); // '1000010'
254n.toString(16); // 'fe'
-10n.toString(2); // -1010'
-0xffn.toString(2); // '-11111111'
```

### Negative-zero `BigInt`

没有负零 `BigInt`,因为整数中没有负零。`-0.0` 是一个 IEEE 浮点概念,只出现在 JavaScript {{jsxref("Number")}} 类型中。

```js
(-0n).toString(); // '0'
(-0n).toString(); // '0'
BigInt(-0).toString(); // '0'
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ console.log(bigint64.length); // 2
console.log(bigint64.BYTES_PER_ELEMENT); // 8

// From an array
var arr = new BigInt64Array([21n,31n]);
var arr = new BigInt64Array([21n, 31n]);
console.log(arr[1]); // 31n

// From another TypedArray
Expand All @@ -116,7 +116,9 @@ var buffer = new ArrayBuffer(32);
var z = new BigInt64Array(buffer, 0, 4);

// From an iterable
var iterable = function*(){ yield* [1n, 2n, 3n]; }();
var iterable = (function* () {
yield* [1n, 2n, 3n];
})();
var bigint64 = new BigInt64Array(iterable);
// BigInt64Array[1n, 2n, 3n]
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ console.log(biguint64.length); // 2
console.log(biguint64.BYTES_PER_ELEMENT); // 8

// From an array
var arr = new BigUint64Array([21n,31n]);
var arr = new BigUint64Array([21n, 31n]);
console.log(arr[1]); // 31n

// From another TypedArray
Expand All @@ -133,7 +133,9 @@ var buffer = new ArrayBuffer(32);
var z = new BigUint64Array(buffer, 0, 4);

// From an iterable
var iterable = function*(){ yield* [1n, 2n, 3n]; }();
var iterable = (function* () {
yield* [1n, 2n, 3n];
})();
var biguint64 = new BigUint64Array(iterable);
// BigUint64Array[1n, 2n, 3n]
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ Boolean(value)
```js
const bZero = new Boolean(0);
const bNull = new Boolean(null);
const bEmptyString = new Boolean('');
const bEmptyString = new Boolean("");
const bfalse = new Boolean(false);

typeof bfalse // "object"
Boolean(bfalse) // true
typeof bfalse; // "object"
Boolean(bfalse); // true
```

请注意,用 `Boolean()``Boolean` 对象转换为原始值的结果总是 `true`,即使该对象的值为 `false`。因此,总是建议避免构造 `Boolean` 包装对象。
Expand All @@ -58,16 +58,16 @@ Boolean(bfalse) // true
```js
const bfalse = new Boolean(false);

bfalse.valueOf() // false
bfalse.valueOf(); // false
```

### 使用初始值 true 创建 Boolean 对象

```js
const btrue = new Boolean(true);
const btrueString = new Boolean('true');
const bfalseString = new Boolean('false');
const bSuLin = new Boolean('Su Lin');
const btrueString = new Boolean("true");
const bfalseString = new Boolean("false");
const bSuLin = new Boolean("Su Lin");
const bArrayProto = new Boolean([]);
const bObjProto = new Boolean({});
```
Expand Down
Loading

0 comments on commit 320ff75

Please sign in to comment.