Skip to content

Commit e5b0fc1

Browse files
committed
translate - extend-natives
1 parent 564fc79 commit e5b0fc1

1 file changed

Lines changed: 22 additions & 21 deletions

File tree

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

22
# 組み込みのクラスを拡張する
33

4-
Built-in classes like Array, Map and others are extendable also.
4+
Array、Mapなどの組み込みのクラスも拡張可能です。
55

6-
For instance, here `PowerArray` inherits from the native `Array`:
6+
例えば、ここでは `PowerArray` はネイティブの `Array` を継承しています:
77

88
```js run
9-
// add one more method to it (can do more)
9+
// 1つメソッドを追加しています(その他のことももちろん可能です)
1010
class PowerArray extends Array {
1111
isEmpty() {
1212
return this.length === 0;
@@ -21,21 +21,22 @@ alert(filteredArr); // 10, 50
2121
alert(filteredArr.isEmpty()); // false
2222
```
2323

24-
Please note a very interesting thing. Built-in methods like `filter`, `map` and others -- return new objects of exactly the inherited type. They rely on the `constructor` property to do so.
24+
とても興味深い点に注目してください。`filter``map` といった組み込みのメソッドは、継承された型と同じ型の新しいオブジェクトを返します。そのようにするために、それらは `constructor` を当てにしています。
25+
26+
上の例では、
2527

26-
In the example above,
2728
```js
2829
arr.constructor === PowerArray
2930
```
3031

31-
So when `arr.filter()` is called, it internally creates the new array of results exactly as `new PowerArray`.
32-
That's actually very cool, because we can keep using `PowerArray` methods further o the result.
32+
なので、`arr.filter()` が呼ばれると、内部的には結果の新しい配列は正確に `new PowerArray` として作成します。
33+
結果に対してさらに `PowerArray` が持つメソッドを使用し続けることができるため、これは非常に有用です。
3334

34-
Even more, we can customize that behavior.
35+
さらに、その振る舞いをカスタマイズすることも可能です。
3536

36-
There's a special static getter `Symbol.species`, if exists, it returns the constructor to use in such cases.
37+
特別な静的な getter `Symbol.species` があります。存在する場合、このような場合に使用するコンストラクタを返します。
3738

38-
If we'd like built-in methods like `map`, `filter` will return regular arrays, we can return `Array` in `Symbol.species`, like here:
39+
もし、`map``filter` のような組み込みのメソッドが通常の配列を返してほしい場合、次のように `Symbol.species``Array` を返すようにします:
3940

4041
```js run
4142
class PowerArray extends Array {
@@ -44,7 +45,7 @@ class PowerArray extends Array {
4445
}
4546

4647
*!*
47-
// built-in methods will use this as the constructor
48+
// 組み込みのメソッドは、これをコンストラクタとして使います
4849
static get [Symbol.species]() {
4950
return Array;
5051
}
@@ -54,29 +55,29 @@ class PowerArray extends Array {
5455
let arr = new PowerArray(1, 2, 5, 10, 50);
5556
alert(arr.isEmpty()); // false
5657

57-
// filter creates new array using arr.constructor[Symbol.species] as constructor
58+
// filter はコンストラクタとして arr.constructor[Symbol.species] を使って新しい配列を作ります
5859
let filteredArr = arr.filter(item => item >= 10);
5960

6061
*!*
61-
// filteredArr is not PowerArray, but Array
62+
// filteredArr PowerArray ではなく Array です
6263
*/!*
6364
alert(filteredArr.isEmpty()); // Error: filteredArr.isEmpty is not a function
6465
```
6566

66-
As you can see, now `.filter` returns `Array`. So the extended functionality is not passed any further.
67+
ご覧の通り、これで `.filter` `Array` を返します。そのため、拡張機能はこれ以上渡されません。
6768

68-
## No static inheritance in built-ins
69+
## 組み込みでは、静的の継承はありません
6970

70-
Built-in objects have their own static methods, for instance `Object.keys`, `Array.isArray` etc.
71+
組み込みのオブジェクトは独自の静的メソッドを持っています。例えば `Object.keys`, `Array.isArray` などです。
7172

72-
And we've already been talking about native classes extending each other: `Array.[[Prototype]] = Object`.
73+
そして、既にそれら拡張するネイティブクラスについて話してきました。: `Array.[[Prototype]] = Object`.
7374

74-
But statics are an exception. Built-in classes don't inherit static properties from each other.
75+
しかし、静的は例外です。組み込みのクラスはお互いから静的なプロパティを継承しません。
7576

76-
In other words, the prototype of build-in constructor `Array` does not point to `Object`. This way `Array` and `Date` do not have `Array.keys` or `Date.keys`. And that feels natural.
77+
つまり、組み込みのコンストラクタ `Array` のプロトタイプは `Object` を指していません。 `Array` `Date` `Array.key` あるいは `Date.keys` を持っていません。これは自然なことのように感じます。
7778

78-
Here's the picture structure for `Date` and `Object`:
79+
これは、`Date` `Object` の構造のイメージです:
7980

8081
![](object-date-inheritance.png)
8182

82-
Note, there's no link between `Date` and `Object`. Both `Object` and `Date` exist independently. `Date.prototype` inherits from `Object.prototype`, but that's all.
83+
`Date` `Object` の間に繋がりはないことに注目してください。`Object` `Date` は両方とも独立して存在します。`Date.prototype` `Object.prototype` を継承していますが、それだけです。

0 commit comments

Comments
 (0)