Symbol.isConcatSpreadable

Symbol.isConcatSpreadable 정적 데이터 속성은 잘 알려진 심볼 @@isConcatSpreadable을 나타냅니다. Array.prototype.concat() 메서드는 연결된 각 객체에서 이 심볼을 찾아 유사 배열과 객체로 취급하고 해당 배열 요소로 평탄화해야 하는지 여부를 결정합니다.

시도해보기

잘 알려진 심볼 @@isConcatSpreadable.

Property attributes of Symbol.isConcatSpreadable
Writable 불가능
Enumerable 불가능
Configurable 불가능

설명

@@isConcatSpreadable 심볼(Symbol.isConcatSpreadable)은 자체 또는 상속된 속성으로 정의할 수 있으며, 그 값은 불리언입니다. 아래와 같이 배열 및 배열 유사 객체에 대한 동작을 제어할 수 있습니다.

  • 배열 객체의 경우 기본 동작은 요소를 펼치는(평평하게 하는) 것입니다. Symbol.isConcatSpreadable은 이러한 경우 평탄화를 피할 수 있습니다.
  • 배열과 유사한 객체의 경우 기본 동작은 펼치거나 평평하게 하지 않는 것입니다. Symbol.isConcatSpreadable은 이러한 경우 평탄화를 강제할 수 있습니다.

예제

배열

기본적으로 Array.prototype.concat()는 여러 배열을 하나의 결과로 펼칩니다(평평하게 만듭니다).

js
const alpha = ["a", "b", "c"];
const numeric = [1, 2, 3];

const alphaNumeric = alpha.concat(numeric);

console.log(alphaNumeric); // 결과: ['a', 'b', 'c', 1, 2, 3]

Symbol.isConcatSpreadablefalse로 설정하면 기본 동작을 비활성화할 수 있습니다.

js
const alpha = ["a", "b", "c"];
const numeric = [1, 2, 3];

numeric[Symbol.isConcatSpreadable] = false;
const alphaNumeric = alpha.concat(numeric);

console.log(alphaNumeric); // Result: ['a', 'b', 'c', [1, 2, 3] ]

유사 배열 객체

유사 배열 객체에서는 기본 동작이 평탄화시키지 않는 것입니다. 배열을 평탄화시키고 싶다면 Symbol.isConcatSpreadabletrue로 설정할 필요가 있습니다.

js
const x = [1, 2, 3];

const fakeArray = {
  [Symbol.isConcatSpreadable]: true,
  length: 2,
  0: "hello",
  1: "world",
};

x.concat(fakeArray); // [1, 2, 3, "hello", "world"]

참고: length 속성은 추가할 객체 속성의 수를 제어하는 데 사용됩니다. 위의 예제에서 length:2는 2개의 속성이 추가되었다는 것을 가리킵니다.

명세서

Specification
ECMAScript Language Specification
# sec-symbol.isconcatspreadable

브라우저 호환성

BCD tables only load in the browser

같이 보기