非常教程

JavaScript参考手册

符号 | Symbol

Symbol.species

Symbol.species 是指定一个构造函数创建派生对象的函数值属性。

| Symbol.species 属性的属性特性: |

|:----|

| Writable | no |

| Enumerable | no |

| Configurable | no |

描述

species 访问器属性允许子类覆盖对象的默认构造函数。

示例

您可能想要返回Array派生数组类中的对象MyArray。例如,当使用诸如map()返回默认构造函数的方法时,您希望这些方法返回父Array对象而不是MyArray对象。物种符号可以让你做到这一点:

class MyArray extends Array {
  // Overwrite species to the parent Array constructor
  static get [Symbol.species]() { return Array; }
}
var a = new MyArray(1,2,3);
var mapped = a.map(x => x * x);

console.log(mapped instanceof MyArray); // false
console.log(mapped instanceof Array);   // true

规范

Specification

Status

Comment

ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Symbol.species' in that specification.

Standard

Initial definition.

ECMAScript Latest Draft (ECMA-262)The definition of 'Symbol.species' in that specification.

Living Standard

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer/Edge

Opera

Safari

Basic support

51

(Yes)

41 (41)

14

38

10

Feature

Android

Chrome for Android

Edge

Firefox Mobile (Gecko)

IE/Edge Mobile

Opera Mobile

Safari Mobile

Basic support

No support

5

(Yes)

41.0 (41)

14

38

10

JavaScript

JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络