Reflect.apply()
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์โฉ.
Reflect.apply()
์ ์ ๋ฉ์๋๋ ๋์ ํจ์๋ฅผ ์ฃผ์ด์ง ๋งค๊ฐ๋ณ์๋ก ํธ์ถํฉ๋๋ค.
์๋ํด ๋ณด๊ธฐ
console.log(Reflect.apply(Math.floor, undefined, [1.75]));
// Expected output: 1
console.log(
Reflect.apply(String.fromCharCode, undefined, [104, 101, 108, 108, 111]),
);
// Expected output: "hello"
console.log(
Reflect.apply(RegExp.prototype.exec, /ab/, ["confabulation"]).index,
);
// Expected output: 4
console.log(Reflect.apply("".charAt, "ponies", [3]));
// Expected output: "i"
๊ตฌ๋ฌธ
Reflect.apply(target, thisArgument, argumentsList);
๋งค๊ฐ๋ณ์
target
-
ํธ์ถํ ๋์ ํจ์.
thisArgument
-
ํธ์ถ์์
target
์this
๋ก ์ฌ์ฉํ ๊ฐ. argumentsList
-
target
์ ํธ์ถํ ๋ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ ๋ฐฐ์ดํ ๊ฐ์ฒด.
๋ฐํ ๊ฐ
์ฃผ์ด์ง this
๊ฐ๊ณผ ๋งค๊ฐ๋ณ์๋ก ๋์ ํจ์๋ฅผ ํธ์ถํ ๊ฒฐ๊ณผ.
์์ธ
target
์ด ํธ์ถ ๊ฐ๋ฅํ ๊ฐ์ฒด๊ฐ ์๋๋ฉด TypeError
.
์ค๋ช
ES5์์๋ Function.prototype.apply()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด, ํจ์๋ฅผ ํธ์ถํ ๋ this
๊ฐ์ ์ง์ ํ๊ฑฐ๋ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐฐ์ด(๋๋ ๋ฐฐ์ดํ ๊ฐ์ฒด)์์ ๋๊ฒจ์ค ์ ์์์ต๋๋ค.
Function.prototype.apply.call(Math.floor, undefined, [1.75]);
Reflect.apply()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด ๊ฐ์ ์์
์ ๋ ์ฝ๊ณ ์ ๋ คํ๊ฒ ์ํํ ์ ์์ต๋๋ค.
์์
Reflect.apply()
์ฌ์ฉํ๊ธฐ
Reflect.apply(Math.floor, undefined, [1.75]);
// 1;
Reflect.apply(String.fromCharCode, undefined, [104, 101, 108, 108, 111]);
// "hello"
Reflect.apply(RegExp.prototype.exec, /ab/, ["confabulation"]).index;
// 4
Reflect.apply("".charAt, "ponies", [3]);
// "i"
๋ช ์ธ
Specification |
---|
ECMAScriptยฎ 2026 Language Specification # sec-reflect.apply |
๋ธ๋ผ์ฐ์ ํธํ์ฑ
Loadingโฆ