ArrayBuffer[@@物种]

ArrayBuffer[@@species] 静态访问器属性返回用于构造数组缓冲区方法返回值的构造函数。

¥The ArrayBuffer[@@species] static accessor property returns the constructor used to construct return values from array buffer methods.

警告:@@species 的存在允许执行任意代码并可能产生安全漏洞。它还使某些优化变得更加困难。引擎实现者是 研究是否删除此功能。如果可能的话,避免依赖它。

¥Warning: The existence of @@species allows execution of arbitrary code and may create security vulnerabilities. It also makes certain optimizations much harder. Engine implementers are investigating whether to remove this feature. Avoid relying on it if possible.

语法

¥Syntax

js
ArrayBuffer[Symbol.species]

返回值

¥Return value

调用 get @@species 的构造函数 (this) 的值。返回值用于从创建新数组缓冲区的数组缓冲区方法构造返回值。

¥The value of the constructor (this) on which get @@species was called. The return value is used to construct return values from array buffer methods that create new array buffers.

描述

¥Description

@@species 访问器属性返回 ArrayBuffer 对象的默认构造函数。子类构造函数可以重写它以更改构造函数赋值。默认实现基本上是:

¥The @@species accessor property returns the default constructor for ArrayBuffer objects. Subclass constructors may override it to change the constructor assignment. The default implementation is basically:

js
// Hypothetical underlying implementation for illustration
class ArrayBuffer {
  static get [Symbol.species]() {
    return this;
  }
}

由于这种多态实现,派生子类的 @@species 也会默认返回构造函数本身。

¥Because of this polymorphic implementation, @@species of derived subclasses would also return the constructor itself by default.

js
class SubArrayBuffer extends ArrayBuffer {}
SubArrayBuffer[Symbol.species] === SubArrayBuffer; // true

当调用不改变现有对象但返回新数组缓冲区实例(例如 slice())的数组缓冲区方法时,将访问该对象的 constructor[@@species]。返回的构造函数将用于构造数组缓冲区方法的返回值。

¥When calling array buffer methods that do not mutate the existing object but return a new array buffer instance (for example, slice()), the object's constructor[@@species] will be accessed. The returned constructor will be used to construct the return value of the array buffer method.

示例

¥Examples

普通对象中的物种

¥Species in ordinary objects

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

¥The @@species property returns the default constructor function, which is the ArrayBuffer constructor for ArrayBuffer.

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

派生对象中的物种

¥Species in derived objects

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

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

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

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看