Map[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.

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

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

语法

¥Syntax

js
Map[Symbol.species]

返回值

¥Return value

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

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

描述

¥Description

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

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

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

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

示例

¥Examples

普通对象中的物种

¥Species in ordinary objects

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

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

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

派生对象中的物种

¥Species in derived objects

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

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

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

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also