TypedArray.from()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.
TypedArray.from()
정적 메서드는 순회 가능한 객체 혹은 유사 배열로부터 새로운 형식화 배열을 생성합니다. 이 메서드는 Array.from()
와 거의 같습니다.
시도해 보기
const uint16 = Int16Array.from("12345");
console.log(uint16);
// Expected output: Int16Array [1, 2, 3, 4, 5]
구문
TypedArray.from(arrayLike, mapFn)
TypedArray.from(arrayLike, mapFn, thisArg)
TypedArray
는 아래 중 하나입니다.
매개변수
arrayLike
-
형식화 배열로 변환할 순회 가능한 객체 혹은 유사 객체.
mapFn
Optional-
입력된 배열의 모든 요소에 대해 호출할 함수입니다. 이 함수를 제공하면 배열에 추가할 모든 값이 먼저 이 함수를 통과하고
mapFn
의 반환 값이 형식화 배열에 입력 값 대신 추가됩니다. 이 함수는 다음 인수를 사용하여 호출됩니다. thisArg
Optional-
mapFn
을 실행할 때this
로 사용할 값입니다.
반환 값
새로운 TypedArray
인스턴스입니다.
설명
자세한 내용은 Array.from()
을 참고하시기 바랍니다.
Array.from()
와 TypedArray.from()
사이에는 미묘한 차이가 있습니다(참고: 아래에 언급된 this
값은 mapFn
을 호출하는 데 사용된 thisArg
인수가 아니라 TypedArray.from()
가 호출된 this
값입니다).
TypedArray.from()
의this
값이 생성자가 아닌 경우,TypedArray.from()
는TypeError
를 발생시키고,Array.from()
는 기본적으로 새Array
를 생성합니다.this
가 생성하는 객체는TypedArray
인스턴스여야 하며,Array.from()
는this
값을 어떤 객체로든 생성할 수 있습니다.source
매개변수가 반복자인 경우TypedArray.from()
은 먼저 반복자에서 모든 값을 수집한 다음 그 개수를 사용하여this
의 인스턴스를 생성하고 마지막으로 인스턴스에 값을 설정합니다.Array.from()
은 반복자로부터 값을 받을 때마다 각 값을 설정한 다음 마지막에length
를 설정합니다.TypedArray.from()
은[[Set]]
을 사용하는 반면Array.from()
은[[DefineOwnProperty]]
을 사용합니다. 따라서Proxy
객체로 작업할 때는handler.set()
대신handler.defineProperty()
를 호출하여 새 요소를 생성합니다.Array.from()
이 반복자가 아닌 유사 배열을 가져올 때, 중간에 빠진 부분을 존중합니다.TypedArray.from()
은 결과가 희소 배열이 안되도록 보장합니다.
예제
순회 가능한 객체(Set)으로부터
const s = new Set([1, 2, 3]);
Uint8Array.from(s);
// Uint8Array [ 1, 2, 3 ]
문자열로부터
Int16Array.from("123");
// Int16Array [ 1, 2, 3 ]
Map과 화살표 함수와 함께 사용
요소를 조작하는 Map 함수로서 화살표 함수 사용하기
Float32Array.from([1, 2, 3], (x) => x + x);
// Float32Array [ 2, 4, 6 ]
일련의 번호 생성
Uint8Array.from({ length: 5 }, (v, k) => k);
// Uint8Array [ 0, 1, 2, 3, 4 ]
TypeArray가 아닌 생성자에서 from() 호출하기
from()
의 this
값은 TypedArray
인스턴스를 반환하는 생성자여야 합니다.
function NotArray(len) {
console.log("NotArray called with length", len);
}
Int8Array.from.call({}, []); // TypeError: #<Object> is not a constructor
Int8Array.from.call(NotArray, []);
// NotArray는 length 0으로 호출되었습니다.
// TypeError: Method %TypedArray%.from called on incompatible receiver #<NotArray>
function NotArray2(len) {
console.log("NotArray2 called with length", len);
return new Uint8Array(len);
}
console.log(Int8Array.from.call(NotArray2, [1, 2, 3]));
// NotArray2는 length 3으로 호출되었습니다.
// Uint8Array(3) [ 1, 2, 3 ]
명세서
Specification |
---|
ECMAScript® 2025 Language Specification # sec-%typedarray%.from |