JavaScript_proxy-and-reflect-1.md
๐กJavascript Study
๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ Deep Dive + Mozilla
๐ Proxy & Reflect: part1
์๋ฐ์คํฌ๋ฆฝํธ https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Proxy#%ED%94%84%EB%9D%BC%EC%9D%B4%EB%B9%97_%EC%86%8D%EC%84%B1_%ED%8F%AC%EC%9B%8C%EB%94%A9_%EC%97%86%EC%9D%8C > https://replit.com/@dusunax/javascript#index.js
- Proxy์ Reflect
1. Reflect
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Reflect
-
์ค๊ฐ์ ๊ฐ๋ก์ฑ ์ ์๋ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ ๋ด์ฅ ๊ฐ์ฒด
-
์ ์ ๋ฉ์๋: ํ๋ก์ ๋ฉ์๋์ ์ด๋ฆ ๊ฐ์
-
construct์ ๊ฒฝ์ฐ, Object.create()์ ์ฐจ์ด : https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Reflect/construct#reflect.construct_vs_object.create
- Reflect.construct()๋ฅผ ํธ์ถํ๋ฉด newTarget ์กด์ฌ ์ newTarget, ์๋๋ฉด target์ ๊ฐ๋ฆฌํด (undefined๋ฅผ ๊ฐ๋ฆฌํค์ง ์๋๋ค.)
-
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"
๋ฉ์๋
- proxy์ ์ ์ ๋ฉ์๋๋ช ๋์ผ
| ๋ฉ์๋ | ๋ด์ฉ | ๊ตฌ๋ฌธ, ๋น๊ณ | | ------------------------ | ----------------------------------------------------------------------------- | -------------------------------------------------------- | | apply | ๋์ ํจ์๋ฅผ ์ฃผ์ด์ง ๋งค๊ฐ๋ณ์๋ก ํธ์ถ // this ๊ฐ์ ์ง์ ํ๊ฑฐ๋, ๋งค๊ฐ๋ณ์๋ฅผ ๋๊ธฐ๊ธฐ | Reflect.apply(target, thisArgument, argumentsList); | | construct | new ์ฐ์ฐ์, ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ๋ณ ๊ธธ์ด๋ก ํธ์ถ ๊ฐ๋ฅ | Reflect.construct(target, argumentsList[, newTarget]) | | defineProperty | ํค&๊ฐ ์ค์ , Boolean ๋ฐํ | Reflect.defineProperty(target, propertyKey, attributes); | | deleteProperty | ํจ์๋ก object๋ก๋ถํฐ property ์ญ์ | Reflect.deleteProperty(target, propertyKey); | | get | ์์ฑ ๊ฐ ๋ฐํ | Reflect.get(target, propertyKey[, receiver]) | | getOwnPropertyDescriptor | ์์ฑ์ด ๋์ ๊ฐ์ฒด์ ์กด์ฌํ๋ฉด ์์ ์ ๋ฐํ, ์๋๋ฉด undefind | Reflect.getOwnPropertyDescriptor(target, propertyKey); | | getPrototypeOf | ํ๋กํ ํ์ ๋ฐํ | Reflect.getPrototypeOf(target); | | has | in ์ฐ์ฐ์, return Boolean | Reflect.has(target, propertyKey); | | isExtensible | ๊ฐ์ฒด ํ์ฅ ์ฌ๋ถ, return Boolean | Reflect.isExtensible(target); | | ownKeys | ๊ฐ์ฒด์ ์์ฒด ์์ฑ ํค ๋ฐฐ์ด ๋ฐํ | Reflect.ownKeys(target); | | preventExtensions | ํ์ฅ(๊ฐ์ฒด ์ถ๊ฐ) ๊ฐ๋ฅ ์ฌ๋ถ | Reflect.preventExtensions(target); | | set | ๊ฐ์ฒด ์์ฑ์ ๊ฐ ๊ฒฐ์ | Reflect.set(target, propertyKey, value[, receiver]) | | setPrototypeOf | ๊ฐ์ฒด์ ํ๋กํ ํ์ ์ ๋ค๋ฅธ ๊ฐ์ฒด๋ null๋ก ๋ฐ๊ฟ ์ ์์ | Reflect.setPrototypeOf(target, prototype); |
2. Proxy
- ํ ๊ฐ์ฒด์ ๋ํ ๊ธฐ๋ณธ ์์
์ ๊ฐ๋ก์ฑ๊ณ ์ฌ์ ์ํ๋ ํ๋ก์
- ์์ฑ ์ก์ธ์ค ๊ธฐ๋ก, ์ ๋ ฅ ์ ํจ์ฑ ๊ฒ์ฌ, ํ์ ์ง์ , ์ญ์
- target๊ณผ handler๋ฅผ ์ฌ์ฉํ์ฌ Proxy ์์ฑ
const target = {
message1: "hello",
message2: "everyone",
};
const handler1 = {};
const proxy1 = new Proxy(target, handler1);
- get() ์ฒ๋ฆฌ๊ธฐ == ํธ๋ฉ trap
// get()
const handler2 = {
get(target, prop, receiver) {
return "world";
},
};
const proxy2 = new Proxy(target, handler2);
console.log(proxy2); // { message1: 'hello', message2: 'everyone' }
console.log(proxy2.message1); // world
console.log(proxy2.message2); // world
- reflect์ proxy
const target = {
message1: "hello",
message2: "everyone",
};
const handler3 = {
get(target, prop, receiver) {
if (prop === "message2") {
return "world";
}
return Reflect.get(...arguments); // Reflect ์์ ์, message1์ undefined
},
};
const proxy3 = new Proxy(target, handler3);
console.log(proxy3.message1); // hello
console.log(proxy3.message2); // world
=> Proxy ์์ ์ดํด๋ณด๊ธฐ-1์์ ๊ณ์