SharedArrayBuffer[@@物种]

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

¥The SharedArrayBuffer[@@species] static accessor property returns the constructor used to construct return values from SharedArrayBuffer 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
SharedArrayBuffer[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 buffer.

描述

¥Description

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

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

js
// Hypothetical underlying implementation for illustration
class SharedArrayBuffer {
  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 SharedArrayBuffer {}
SubArrayBuffer[Symbol.species] === SharedArrayBuffer; // true

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

¥When calling array buffer methods that do not mutate the existing array but return a new array buffer instance (for example, slice()), the array'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 属性返回默认构造函数,即 SharedArrayBufferSharedArrayBuffer 构造函数。

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

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

派生对象中的物种

¥Species in derived objects

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

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

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

规范

Specification
ECMAScript Language Specification
# sec-sharedarraybuffer-@@species

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看