From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/array/@@species/index.html | 78 ++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/array/@@species/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/array/@@species/index.html') diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/@@species/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/@@species/index.html new file mode 100644 index 0000000000..cc87927e5e --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/array/@@species/index.html @@ -0,0 +1,78 @@ +--- +title: 'get Array[@@species]' +slug: Web/JavaScript/Reference/Global_Objects/Array/@@species +tags: + - JavaScript + - 原型 + - 数组 + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/Array/@@species +--- +
{{JSRef}}
+ +

Array[@@species] 访问器属性返回 Array 的构造函数。

+ +

语法

+ +
Array[Symbol.species]
+
+ +

返回值

+ +

{{jsxref("Array")}} 的构造函数。

+ +

描述

+ +

species 访问器属性返回 Array 对象的默认构造函数。子类的构造函数可能会覆盖并改变构造函数的赋值。

+ +

示例

+ +

species 属性返回默认构造函数, 它用于 Array 对象的构造函数 Array:

+ +
Array[Symbol.species]; // function Array()
+ +

在继承类的对象中(例如你自定义的数组 MyArray),MyArray 的 species 属性返回的是 MyArray 这个构造函数. 然而你可能想要覆盖它,以便在你继承的对象 MyArray 中返回父类的构造函数 Array :

+ +
class MyArray extends Array {
+  // 重写 MyArray 的 species 属性到父类 Array 的构造函数
+  static get [Symbol.species]() { return Array; }
+}
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('ES6', '#sec-get-array-@@species', 'get Array [ @@species ]')}}{{Spec2('ES6')}}Initial definition.
{{SpecName('ESDraft', '#sec-get-array-@@species', 'get Array [ @@species ]')}}{{Spec2('ESDraft')}}
+ +

浏览器兼容性

+ +
+
+ + +

{{Compat("javascript.builtins.Array.@@species")}}

+
+
+ +

参见

+ + -- cgit v1.2.3-54-g00ecf