get RegExp[@@species]

RegExp[@@species] 访问器属性返回RegExp 的构造器。

语法

RegExp[Symbol.species]

描述

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

示例

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

js
RegExp[Symbol.species]; // 函数 RegExp()

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

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

规范

Specification
ECMAScript Language Specification
# sec-get-regexp-@@species

浏览器兼容性

BCD tables only load in the browser

参见