ArrayBuffer[Symbol.species]
ArrayBuffer[Symbol.species]
静态访问器属性返回用于构造数组缓冲区方法返回值的构造函数。
¥The ArrayBuffer[Symbol.species]
static accessor property returns the constructor used to construct return values from array buffer methods.
警告:
[Symbol.species]
的存在允许执行任意代码并可能产生安全漏洞。它还使某些优化变得更加困难。引擎实现者是 研究是否删除此功能。如果可能的话,避免依赖它。¥Warning: The existence of
[Symbol.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.
语法
返回值
描述
¥Description
[Symbol.species]
访问器属性返回 ArrayBuffer
对象的默认构造函数。子类构造函数可以重写它以更改构造函数赋值。默认实现基本上是:
¥The [Symbol.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:
// Hypothetical underlying implementation for illustration
class ArrayBuffer {
static get [Symbol.species]() {
return this;
}
}
由于这种多态实现,派生子类的 [Symbol.species]
也会默认返回构造函数本身。
¥Because of this polymorphic implementation, [Symbol.species]
of derived subclasses would also return the constructor itself by default.
class SubArrayBuffer extends ArrayBuffer {}
SubArrayBuffer[Symbol.species] === SubArrayBuffer; // true
当调用不改变现有对象但返回新数组缓冲区实例(例如 slice()
)的数组缓冲区方法时,将访问该对象的 constructor[Symbol.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[Symbol.species]
will be accessed. The returned constructor will be used to construct the return value of the array buffer method.
示例
普通对象中的物种
派生对象中的物种
¥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:
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 |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also