aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/regexp/@@species/index.html
blob: aa8a496a1136e1feded0d22def766e2aa7e66472 (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
---
title: 'get RegExp[@@species]'
slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@species
translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@species
---
<div>{{JSRef}}</div>

<p><code><strong>RegExp[@@species]</strong></code> 访问器属性返回<code>RegExp</code> 的构造器。</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">RegExp[Symbol.species]
</pre>

<h2 id="描述">描述</h2>

<p><code>species</code> 访问器属性返回 <code>RegExp</code> 对象的默认构造器。子类构造器可能会覆盖它,来修改构造器的指派。</p>

<h2 id="示例">示例</h2>

<p><code>species</code>属性返回默认构造器函数,它是用于<code>RegExp</code> 对象的<code>RegExp</code>构造器:</p>

<pre class="brush: js">RegExp[Symbol.species]; // 函数 RegExp()</pre>

<p>在派生的正则类(也就是你自定义的正则类 <code>MyRegExp</code>)中,<code>MyRegExp</code> 的 species 是 <code>MyRegExp</code> 构造器。但是,你可能希望覆盖它,以便在你的派生类方法中,返回 <code>RegExp</code> 父类对象:</p>

<pre class="brush: js">class MyRegExp extends RegExp {
  // 将 MyRegExp species 覆盖为 RegExp 父类构造器
  static get [Symbol.species]() { return RegExp; }
}</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-get-regexp-@@species', 'get RegExp [ @@species ]')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>初始定义。</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-get-regexp-@@species', 'get RegExp [ @@species ]')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

{{Compat}}

<h2 id="另见">另见</h2>

<ul>
 <li>{{jsxref("RegExp")}}</li>
 <li>{{jsxref("Symbol.species")}}</li>
</ul>