aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/global_objects/symbol/species/index.html
blob: c192f1b565a3a989ab56feb1e5f573acda4ce6e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
---
title: Symbol.species
slug: Web/JavaScript/Reference/Global_Objects/Symbol/species
tags:
  - ECMAScript 2015
  - JavaScript
  - Property
  - Symbol
translation_of: Web/JavaScript/Reference/Global_Objects/Symbol/species
---
<div>{{JSRef}}</div>

<p>ウェルノウンシンボルの <code><strong>Symbol.species</strong></code> は、コンストラクター関数が派生オブジェクトを生成する際に使用する関数値プロパティを指定します。</p>

<div>{{EmbedInteractiveExample("pages/js/symbol-species.html")}}</div>

<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div>

<h2 id="Description" name="Description">解説</h2>

<p><code>species</code> アクセサープロパティを使用すると、オブジェクトの生成に使われるデフォルトコンストラクターを上書きすることができます。</p>

<p>{{js_property_attributes(0,0,0)}}</p>

<h2 id="Examples" name="Examples"></h2>

<h3 id="Using_species" name="Using_species">species の使用</h3>

<p>{{jsxref("Array")}} オブジェクトを派生した配列クラス <code>MyArray</code> で返したいことがあるでしょう。例えば、デフォルトのコンストラクターを返す {{jsxref("Array.map", "map()")}} のようなメソッドを使用している場合、これらのメソッドは <code>MyArray</code> オブジェクトではなく親の <code>Array</code> オブジェクトを返したいとします。 <code>species</code> シンボルでこれを実現することができます。</p>

<pre class="brush: js notranslate">class MyArray extends Array {
  // Overwrite species to the parent Array constructor
  static get [Symbol.species]() { return Array; }
}
let a = new MyArray(1,2,3);
let mapped = a.map(x =&gt; x * x);

console.log(mapped instanceof MyArray); // false
console.log(mapped instanceof Array);   // true
</pre>

<h2 id="Specifications" name="Specifications">仕様書</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">仕様書</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-symbol.species', 'Symbol.species')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>

<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div>

<p>{{Compat("javascript.builtins.Symbol.species")}}</p>

<h2 id="See_also" name="See_also">関連情報</h2>

<ul>
 <li>{{jsxref("Map.@@species", "Map[@@species]")}}</li>
 <li>{{jsxref("Set.@@species", "Set[@@species]")}}</li>
</ul>