Set[Symbol.species]

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Set[Symbol.species] 静态访问器属性是未使用的访问器属性,指定如何复制 Set 对象。

¥The Set[Symbol.species] static accessor property is an unused accessor property specifying how to copy Set objects.

语法

¥Syntax

js
Set[Symbol.species]

返回值

¥Return value

调用 get [Symbol.species] 的构造函数 (this) 的值。返回值用于构造复制的 Set 实例。

¥The value of the constructor (this) on which get [Symbol.species] was called. The return value is used to construct copied Set instances.

描述

¥Description

[Symbol.species] 访问器属性返回 Set 对象的默认构造函数。子类构造函数可以重写它以更改构造函数赋值。

¥The [Symbol.species] accessor property returns the default constructor for Set objects. Subclass constructors may override it to change the constructor assignment.

注意:目前所有 Set 方法均未使用此属性。

¥Note: This property is currently unused by all Set methods.

示例

¥Examples

普通对象中的物种

¥Species in ordinary objects

[Symbol.species] 属性返回默认构造函数,即 SetSet 构造函数。

¥The [Symbol.species] property returns the default constructor function, which is the Set constructor for Set.

js
Set[Symbol.species]; // function Set()

派生对象中的物种

¥Species in derived objects

在自定义 Set 子类(例如 MySet)的实例中,MySet 种类是 MySet 构造函数。但是,你可能想要覆盖它,以便在派生类方法中返回父 Set 对象:

¥In an instance of a custom Set subclass, such as MySet, the MySet species is the MySet constructor. However, you might want to overwrite this, in order to return parent Set objects in your derived class methods:

js
class MySet extends Set {
  // Overwrite MySet species to the parent Set constructor
  static get [Symbol.species]() {
    return Set;
  }
}

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also