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์—์„œ ๊ณ„์†