TypedArray.prototype.at()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2022ë 3ì.
TypedArray ì¸ì¤í´ì¤ì at() ë©ìëë ì ì ê°ì ë°ì í´ë¹ ì¸ë±ì¤ì ìë í목ì ë°ííë©°, ìì ì ìì ìì ì ì를 모ë íì©í©ëë¤. ìì ì ìë ë°°ì´ì ë§ì§ë§ í목ììë¶í° ììì¼ë¡ ì
ëë¤. ì´ ë©ìëë Array.prototype.at()ì ëì¼í ìê³ ë¦¬ì¦ì ê°ì§ëë¤.
ìëí´ ë³´ê¸°
const int8 = new Int8Array([0, 10, -10, 20, -30, 40, -50]);
let index = 1;
console.log(`An index of ${index} returns the item ${int8.at(index)}`);
// Expected output: "An index of 1 returns the item 10"
index = -2;
console.log(`An index of ${index} returns the item ${int8.at(index)}`);
// Expected output: "An index of -2 returns the item 40"
구문
at(index)
매ê°ë³ì
index-
ë°íí ë°°ì´ ììì 0ë¶í° ììíë ì¸ë±ì¤ë¡, ì ìë¡ ë³íë©ëë¤. ìì ì¸ë±ì¤ë íìí ë°°ì´ ëë¶í° ììì¼ë¡ ì ëë¤.
index < 0ì¸ ê²½ì°,index + array.lengthë¡ ì ê·¼í©ëë¤.
ë°í ê°
주ì´ì§ ì¸ë±ì¤ì ì¼ì¹íë íìí ë°°ì´ì ììì
ëë¤. index < -array.length ëë index >= array.lengthì¸ ê²½ì°, í´ë¹ ìì±ì ì ê·¼íì§ ìê³ íì undefined를 ë°íí©ëë¤.
ì¤ëª
ë³´ë¤ ìì¸í ì ë³´ë Array.prototype.at()를 ì°¸ê³ íì기 ë°ëëë¤. ì´ ë©ìëë ë²ì©ì ì´ì§ ìì¼ë©°, íìí ë°°ì´ ì¸ì¤í´ì¤ììë§ í¸ì¶í ì ììµëë¤.
ìì
>ë°°ì´ì ë ê° ë°í
ìë ìì ë 주ì´ì§ ë°°ì´ìì 맨 ë§ì§ë§ì ìì¹í ê°ì ë°ííë í¨ìì ëë¤.
const uint8 = new Uint8Array([1, 2, 4, 7, 11, 18]);
// 주ì´ì§ ë°°ì´ìì ë§ì§ë§ ìì를 ë°ííë í¨ì
function returnLast(arr) {
return arr.at(-1);
}
const lastItem = returnLast(uint8);
console.log(lastItem); // 18
ë¹êµ ë©ìë
ìë ìì ììë TypedArrayì ë¤ìì ë ë²ì§¸ ìì를 ì ííë ë¤ìí ë°©ë²ì ë¹êµí©ëë¤. ìëì 모ë ë°©ë²ì´ ì í¨íì§ë§, at() ë©ìëì ê°ê²°ì±ê³¼ ê°ë
ì±ì ê°ì¡°í©ëë¤.
// ëª ê°ì§ ê°ì´ ìë íìí ë°°ì´
const uint8 = new Uint8Array([1, 2, 4, 7, 11, 18]);
// length ìì± ì¬ì©í기
const lengthWay = uint8[uint8.length - 2];
console.log(lengthWay); // 11
// slice() ë©ìë ì¬ì©í기. ë°°ì´ì´ ë°íëë걸 ëª
ì¬íì¸ì
const sliceWay = uint8.slice(-2, -1);
console.log(sliceWay[0]); // 11
// at() ë©ìë ì¬ì©í기
const atWay = uint8.at(-2);
console.log(atWay); // 11
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2027 Language Specification > # sec-%typedarray%.prototype.at > |