This page was translated from English by the community.Learn more and join the MDN Web Docs community.
Symbol.species
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2016년 9월.
Symbol.species 정적 데이터 속성은잘 알려진 심볼Symbol.species을 나타냅니다. 객체의 복사본을 생성하는 메서드는 생성자 함수가 복사본을 만들 때 사용할 객체에서 이 심볼을 검색할 수 있습니다.
경고 :Symbol.species가 존재하면 임의의 코드가 실행될 수 있으며 보안 취약점이 발생할 수 있습니다. 또한 특정 최적화를 훨씬 더 어렵게 만듭니다. 엔진 구현자는이 기능을 제거할지 여부를 조사하고 있습니다. 가능하면 이 기능을 사용하지 마세요.
In this article
시도해 보기
class Array1 extends Array { static get [Symbol.species]() { return Array; }}const a = new Array1(1, 2, 3);const mapped = a.map((x) => x * x);console.log(mapped instanceof Array1);// Expected output: falseconsole.log(mapped instanceof Array);// Expected output: true값
잘 알려진 심볼Symbol.species.
Property attributes ofSymbol.species | |
|---|---|
| 쓰기 가능 | 불가능 |
| 열거 가능 | 불가능 |
| 설정 가능 | 불가능 |
설명
Symbol.species 접근자 속성을 사용하면 하위 클래스가 객체의 기본 생성자를 재정의할 수 있습니다. 이는 인스턴스 복사 방법에 대한 프로토콜을 지정합니다. 예를 들어, 배열의 복사 메서드를 사용하는 경우map()와 같이map() 메서드는instance.constructor[Symbol.species]를 사용하여 새 배열을 구성하기 위한 생성자를 가져옵니다. 자세한 내용은하위 클래스 내장를 참조하십시오.
Symbol.species의 모든 내장 구현은 현재 인스턴스의 생성자인this 값을 반환합니다. 따라서 메서드를 복사하면 기본 클래스가 아닌 파생 클래스의 인스턴스를 생성할 수 있습니다(예:map()은 원래 배열과 동일한 유형의 배열을 반환합니다).
예제
>species 사용하기
파생된 배열 클래스MyArray에서Array 객체를 반환하고 싶을 수 있습니다. 예를 들어 기본 생성자를 반환하는map()와 같은 메서드를 사용할 때 이러한 메서드가MyArray 객체 대신 부모Array 객체를 반환하기를 원할 수 있습니다.species 심볼을 사용하면 이렇게 할 수 있습니다.
class MyArray extends Array { // 부모 배열 생성자로 species 덮어쓰기 static get [Symbol.species]() { return Array; }}const a = new MyArray(1, 2, 3);const mapped = a.map((x) => x * x);console.log(mapped instanceof MyArray); // falseconsole.log(mapped instanceof Array); // true명세서
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-symbol.species> |