Skip to content
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

ja: Format /web/javascript using Prettier (part 6) #14816

Merged
merged 5 commits into from
Aug 15, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function replaceString(oldS, newS, fullS) {
fullS.substring(i + oldS.length, fullS.length);
}
}
return fullS
return fullS;
}

replaceString("World", "Web", "Brave New World");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ slug: Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator

```js
const myAsyncIterable = {
async* [Symbol.asyncIterator]() {
yield "hello";
yield "async";
yield "iteration!";
}
async *[Symbol.asyncIterator]() {
yield "hello";
yield "async";
yield "iteration!";
},
};

(async () => {
for await (const x of myAsyncIterable) {
console.log(x);
// 期待される出力:
// "hello"
// "async"
// "iteration!"
}
for await (const x of myAsyncIterable) {
console.log(x);
// 期待される出力:
// "hello"
// "async"
// "iteration!"
}
})();
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ slug: Web/JavaScript/Reference/Global_Objects/Symbol/description
### description を使用する

```js
Symbol('desc').toString(); // "Symbol(desc)"
Symbol('desc').description; // "desc"
Symbol('').description; // ""
Symbol().description; // undefined
Symbol("desc").toString(); // "Symbol(desc)"
Symbol("desc").description; // "desc"
Symbol("").description; // ""
Symbol().description; // undefined

// well-known symbols
Symbol.iterator.toString(); // "Symbol(Symbol.iterator)"
Symbol.iterator.toString(); // "Symbol(Symbol.iterator)"
Symbol.iterator.description; // "Symbol.iterator"

// global symbols
Symbol.for('foo').toString(); // "Symbol(foo)"
Symbol.for('foo').description; // "foo"
Symbol.for("foo").toString(); // "Symbol(foo)"
Symbol.for("foo").description; // "foo"
```

## 仕様
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ Symbol.for(key);
### Symbol.for() の使用

```js
Symbol.for('foo'); // create a new global symbol
Symbol.for('foo'); // retrieve the already created symbol
Symbol.for("foo"); // create a new global symbol
Symbol.for("foo"); // retrieve the already created symbol

// Same global symbol, but not locally
Symbol.for('bar') === Symbol.for('bar'); // true
Symbol('bar') === Symbol('bar'); // false
Symbol.for("bar") === Symbol.for("bar"); // true
Symbol("bar") === Symbol("bar"); // false

// The key is also used as the description
var sym = Symbol.for('mario');
var sym = Symbol.for("mario");
sym.toString(); // "Symbol(mario)"
```

グローバルシンボルキーや他の (ライブラリコードの) グローバルシンボルとの名前の衝突を避けるために、シンボルの前に接頭辞をつけると良いかもしれません。

```js
Symbol.for('mdn.foo');
Symbol.for('mdn.bar');
Symbol.for("mdn.foo");
Symbol.for("mdn.bar");
```

## 仕様書
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ slug: Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance
```js
class MyArray {
static [Symbol.hasInstance](instance) {
return Array.isArray(instance)
return Array.isArray(instance);
}
}
console.log([] instanceof MyArray); // true
```

```js
function MyArray() { }
function MyArray() {}
Object.defineProperty(MyArray, Symbol.hasInstance, {
value: function(instance) { return Array.isArray(instance); }
value: function (instance) {
return Array.isArray(instance);
},
});
console.log([] instanceof MyArray); // true
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ Symbol は任意で説明文を持つことができますが、これはデバ

```js
// 2 つのシンボルを同じ説明文で使用
let Sym1 = Symbol("Sym")
let Sym2 = Symbol("Sym")
let Sym1 = Symbol("Sym");
let Sym2 = Symbol("Sym");

console.log(Sym1 === Sym2) // "false" を返す
console.log(Sym1 === Sym2); // "false" を返す
// シンボルは固有であることが保証されます。
// 同じ説明文でたくさんのシンボルが生成されても、
// 異なる値を持ちます。
Expand All @@ -35,17 +35,17 @@ JavaScript の大部分の値は文字列への暗黙的な変換に対応して
例を示します。

```js
let Sym = Symbol("Sym")
alert(Sym) // TypeError: Cannot convert a Symbol value to a string
let Sym = Symbol("Sym");
alert(Sym); // TypeError: Cannot convert a Symbol value to a string
```

これは混合しないための「言語ガード」で、これは文字列とシンボルが根本的に異なるため、そして他の型に変換するべきものではないためです。

本当にシンボルを表示したいのであれば、 `.toString()` を呼び出す必要があります。

```js
let Sym = Symbol("Sym")
alert(Sym.toString()) // Symbol(Sym), now it works
let Sym = Symbol("Sym");
alert(Sym.toString()); // Symbol(Sym), now it works
```

または、 `symbol.description` プロパティを使用して、説明文を取得することができます。
Expand Down Expand Up @@ -77,12 +77,12 @@ well-known symbol の例としては、配列風オブジェクトのための {
`Symbol.for(tokenString)` メソッドはレジストリー内のシンボル値を返し、 `Symbol.keyFor(symbolValue)` メソッドはレジストリーからトークンの文字列を返します。この二つは対照的で、下記の結果は `true` です。

```js
Symbol.keyFor(Symbol.for("tokenString")) === "tokenString" // true
Symbol.keyFor(Symbol.for("tokenString")) === "tokenString"; // true
```

## 関連情報

- Wikipedia の [Symbol (programming)](https://en.wikipedia.org/wiki/Symbol_(programming)) (英語)
- Wikipedia の [Symbol (programming)](<https://en.wikipedia.org/wiki/Symbol_(programming)>) (英語)
- [JavaScript データ型とデータ構造](/ja/docs/Web/JavaScript/Data_structures)
- [Symbols in ECMAScript 6](https://2ality.com/2014/12/es6-symbols.html)
- {{jsxref("Symbol")}} (MDN JS リファレンス)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ console.log(alphaNumeric) // Result: ['a', 'b', 'c', [1, 2, 3] ]
配列状のオブジェクトは、既定で展開しません。`Symbol.isConcatSpreadable` 平坦化した配列を取得するには、`true` に設定する必要があります:

```js
let x = [1, 2, 3]
let x = [1, 2, 3];

let fakeArray = {
[Symbol.isConcatSpreadable]: true,
length: 2,
0: 'hello',
1: 'world'
}
0: "hello",
1: "world",
};

x.concat(fakeArray) // [1, 2, 3, "hello", "world"]
x.concat(fakeArray); // [1, 2, 3, "hello", "world"]
```

> **メモ:** `length` プロパティは、追加するオブジェクトプロパティの数を制御するために使用されます。上記の例では、`length:2` は 2 つのプロパティを追加する必要があることを示しています。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,34 @@ slug: Web/JavaScript/Reference/Global_Objects/Symbol/iterator
次のように独自の反復可能オブジェクトを作成できます。

```js
const myIterable = {}
const myIterable = {};
myIterable[Symbol.iterator] = function* () {
yield 1;
yield 2;
yield 3;
yield 1;
yield 2;
yield 3;
};
[...myIterable] // [1, 2, 3]
[...myIterable]; // [1, 2, 3]
```

または、[計算されたプロパティ](/ja/docs/Web/JavaScript/Reference/Operators/Object_initializer#computed_property_names)を使用して、クラスやオブジェクト内で反復可能オブジェクトを直接定義できます。

```js
class Foo {
*[Symbol.iterator] () {
*[Symbol.iterator]() {
yield 1;
yield 2;
yield 3;
}
}

const someObj = {
*[Symbol.iterator] () {
yield 'a';
yield 'b';
}
}
*[Symbol.iterator]() {
yield "a";
yield "b";
},
};

console.log(...new Foo); // 1, 2, 3
console.log(...new Foo()); // 1, 2, 3
console.log(...someObj); // 'a', 'b'
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ Symbol.keyFor(sym);
### keyFor() の使用

```js
var globalSym = Symbol.for('foo'); // create a new global symbol
var globalSym = Symbol.for("foo"); // create a new global symbol
Symbol.keyFor(globalSym); // "foo"

var localSym = Symbol();
Symbol.keyFor(localSym); // undefined

// well-known symbols are not symbols registered
// in the global symbol registry
Symbol.keyFor(Symbol.iterator) // undefined
Symbol.keyFor(Symbol.iterator); // undefined
```

## 仕様書
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ slug: Web/JavaScript/Reference/Global_Objects/Symbol/match
以下のコードは {{jsxref("TypeError")}} を投げます。

```js
'/bar/'.startsWith(/bar/);
"/bar/".startsWith(/bar/);

// Throws TypeError, as /bar/ is a regular expression
// and Symbol.match is not modified.
Expand All @@ -33,8 +33,8 @@ slug: Web/JavaScript/Reference/Global_Objects/Symbol/match
```js
var re = /foo/;
re[Symbol.match] = false;
'/foo/'.startsWith(re); // true
'/baz/'.endsWith(re); // false
"/foo/".startsWith(re); // true
"/baz/".endsWith(re); // false
```

## 仕様
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ slug: Web/JavaScript/Reference/Global_Objects/Symbol/matchAll
このシンボルは {{jsxref("String.prototype.matchAll()")}}、特に {{jsxref("RegExp.@@matchAll", "RegExp.prototype[@@matchAll]()")}} で使用されます。以下の 2 つの例は同じ結果を返します。

```js
'abc'.matchAll(/a/);
"abc".matchAll(/a/);

/a/[Symbol.matchAll]('abc');
/a/[Symbol.matchAll]("abc");
```

このメソッドは、{{jsxref("RegExp")}} サブクラス内の一致動作をカスタマイズするために存在します。
Expand All @@ -29,13 +29,12 @@ slug: Web/JavaScript/Reference/Global_Objects/Symbol/matchAll

```js
let re = /[0-9]+/g;
let str = '2016-01-02|2019-03-07';
let str = "2016-01-02|2019-03-07";

const numbers = {
*[Symbol.matchAll] (str) {
for (const n of str.matchAll(/[0-9]+/g))
yield n[0];
}
*[Symbol.matchAll](str) {
for (const n of str.matchAll(/[0-9]+/g)) yield n[0];
},
};

console.log(Array.from(str.matchAll(numbers)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class CustomReplacer {
this.value = value;
}
[Symbol.replace](string) {
return string.replace(this.value, '#!@?');
return string.replace(this.value, "#!@?");
}
}

console.log('football'.replace(new CustomReplacer('foo')));
console.log("football".replace(new CustomReplacer("foo")));
// expected output: "#!@?tball"
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class caseInsensitiveSearch {
}
}

console.log('foobar'.search(new caseInsensitiveSearch('BaR')));
console.log("foobar".search(new caseInsensitiveSearch("BaR")));
// expected output: 3
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ slug: Web/JavaScript/Reference/Global_Objects/Symbol/species
```js
class MyArray extends Array {
// Overwrite species to the parent Array constructor
static get [Symbol.species]() { return Array; }
static get [Symbol.species]() {
return Array;
}
}
let a = new MyArray(1,2,3);
let mapped = a.map(x => x * x);
let a = new MyArray(1, 2, 3);
let mapped = a.map((x) => x * x);

console.log(mapped instanceof MyArray); // false
console.log(mapped instanceof Array); // true
console.log(mapped instanceof Array); // true
```

## 仕様書
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ slug: Web/JavaScript/Reference/Global_Objects/Symbol/split
```js
class ReverseSplit {
[Symbol.split](string) {
const array = string.split(' ');
const array = string.split(" ");
return array.reverse();
}
}

console.log('Another one bites the dust'.split(new ReverseSplit()));
console.log("Another one bites the dust".split(new ReverseSplit()));
// expected output: [ "dust", "the", "bites", "one", "Another" ]
```

Expand Down
Loading