设置[@@物种]

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

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

语法

¥Syntax

js
Set[Symbol.species]

返回值

¥Return value

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

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

描述

¥Description

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

¥The @@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

@@species 属性返回默认构造函数,即 SetSet 构造函数。

¥The @@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