[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from [Back]  [Original]

TypedArray.from() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

TypedArray.from()

Baseline

20169

TypedArray.from() Array.from()

const uint16 = Int16Array.from("12345");

console.log(uint16);
// : Int16Array [1, 2, 3, 4, 5]

js
TypedArray.from(arrayLike, mapFn)
TypedArray.from(arrayLike, mapFn, thisArg)

TypedArray

arrayLike

mapFn

mapFn

element

index

thisArg

mapFn this

TypedArray

Array.from()

Array.from() TypedArray.from() : this TypedArray.from() this mapFn thisArg

  • 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() (dense array)

(Set)

js
const s = new Set([1, 2, 3]);
Uint8Array.from(s);
// Uint8Array [ 1, 2, 3 ]

js
Int16Array.from("123");
// Int16Array [ 1, 2, 3 ]

map

js
Float32Array.from([1, 2, 3], (x) => x + x);
// Float32Array [ 2, 4, 6 ]

js
Uint8Array.from({ length: 5 }, (v, k) => k);
// Uint8Array [ 0, 1, 2, 3, 4 ]

TypedArray from()

from() this TypedArray

js
function NotArray(len) {
  console.log("NotArray called with length", len);
}

Int8Array.from.call({}, []); // TypeError: #<Object> is not a constructor
Int8Array.from.call(NotArray, []);
// NotArray called with length 0
// TypeError: Method %TypedArray%.from called on incompatible receiver #<NotArray>
js
function NotArray2(len) {
  console.log("NotArray2 called with length", len);
  return new Uint8Array(len);
}
console.log(Int8Array.from.call(NotArray2, [1, 2, 3]));
// NotArray2 called with length 3
// Uint8Array(3) [ 1, 2, 3 ]

ECMAScript 2027 LanguageSpecification
# sec-%typedarray%.from


Web Proxy Viewer  |  New URL  |  Original Page