Proxy์™€ Reflect

Proxy์™€ Reflect

๐ŸŠ Proxy Objects

Proxy ๊ฐ์ฒด๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด์˜ proxy๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ, ๊ทธ ๊ฐ์ฒด์˜ ๊ธฐ์ดˆ์ ์ธ operation์„ ๊ฐ€๋กœ์ฑ„์„œ ์žฌ์ •์˜ํ•œ๋‹ค.

Description

  • constructor๋กœ ํ˜ธ์ถœ๋˜๋ฉด, Proxy exotic objecr๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

  • function์œผ๋กœ์„œ ํ˜ธ์ถœ๋˜๋„๋ก ์˜๋„๋˜์ง€ ์•Š์•˜๋‹ค. ๋งŒ์•ฝ, proxy๋ฅผ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ํ˜ธ์ถœํ•˜๋ฉด exeption์ด ๋˜์ ธ์ง„๋‹ค.

Proxy Constructor

  • [[Prototype]]์ด ๋‚ด๋ถ€์ ์œผ๋กœ ์žˆ๊ณ , ๊ทธ ๊ฐ’์€ Function.prototype์ด๋‹ค.

  • prototype ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š๋‹ค. Proxy exotic objects๊ฐ€ ์ดˆ๊ธฐํ™”ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ [[Prototype]]์„ ๋‚ด๋ถ€์ ์œผ๋กœ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

  • ์ฐธ๊ณ  : [[Prototype]] vs prototype ํ”„๋กœํผํ‹ฐ

    • [[Prototype]] : __proto__

      • ๊ฐ์ฒด๋Š” ์ƒ์„ฑ์ž์˜ ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

      • ํ•จ์ˆ˜ ๊ฐ์ฒด๋Š” Function.prototype์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

    • prototype ํ”„๋กœํผํ‹ฐ

      • ํ•จ์ˆ˜๊ฐ์ฒด๋งŒ ๊ฐ–๊ณ  ์žˆ๋Š” ํ”„๋กœํผํ‹ฐ์ด๋‹ค.

      • ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด(Prototype Object)๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

function Person(name) {
  this.name = name;
}

var foo = new Person('Lee');
console.log(Person.__proto__ === Function.prototype); // true

console.log(Person.prototype === foo.__proto__); // true

Proxy ( target, handler )

Proxy๋Š” ๋‘ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋งŒ๋“ค์–ด์ง„๋‹ค.

  • target : proxyํ•˜๊ณ  ์‹ถ์€ ์›๋ž˜์˜ ๊ฐ์ฒด

  • handler : ์–ด๋– ํ•œ operation์„ ๊ฐ€์ ธ์™€์„œ, ์–ด๋–ป๊ฒŒ ์žฌ์ •์˜ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ •์˜ํ•œ ๊ฐ์ฒด

ํ”„๋ก์‹œ ํ•ธ๋“ค๋Ÿฌ ๋ฉ”์„œ๋“œ๋Š” Proxy Handler - MDN ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์ž.

handler.construct()

handler.construct()๋ฉ”์„œ๋“œ๋Š” new operator๋ฅผ ๊ฐ€๋กœ์ฑˆ๋‹ค.

const handler = {
  construct(target, args, newTarget){
    console.log(`called : ${args.join(', ')}`);
    return {value: args[0] * 10}
  }
}
const prox = new Proxy(function(){}, handler)

const proxItem = new prox(1);
console.log(proxItem.value) // 10

handler.apply()

handler.apply()๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ๊ฐ€๋กœ์ฑˆ๋‹ค.

function sum(a, b) {
  return a + b;
}

const handler = {
  apply: function(target, thisArg, argumentsList) {
    console.log(`Calculate sum: ${argumentsList}`);
    // expected output: "Calculate sum: 1,2"

    return target(argumentsList[0], argumentsList[1]) * 10;
  }
};

const proxy1 = new Proxy(sum, handler);

console.log(sum(1, 2));
// expected output: 3
console.log(proxy1(1, 2));
// expected output: 30

์˜ˆ์ œ

const target = {
  msg1: 'hello',
  msg2:'everyone'
};

const handler1 = {};
const proxy1 = new Proxy(target, handler1);

console.log(proxy1.msg1); // hello
console.log(proxy1.msg2); // world

const handler2 = {
  get(target, prop, receiver){
    return "world"
  }
};

const proxy2 = new Proxy(target, handler2);
console.log(proxy2.msg1); // world
console.log(proxy2.msg2); // world


const handler3 = {
  get(target, prop, reciever){
    if(prop === 'msg2'){
      return'world';
    }
    return Reflect.get(...arguments);
  }
}

const proxy3 = new Proxy(target, handler3);

console.log(proxy3.msg1); // hello
console.log(proxy3.msg2); // world

Proxy๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, this binding์ด ๊นŒ๋‹ค๋กœ์šด ๋ถ€๋ถ„์ด๋‹ค. ์–ด๋– ํ•œ ๋ฉ”์†Œ๋“œ์ด๊ฑด this๊ฐ€ Proxy์— ๋ฐ”์ธ๋”ฉ ๋˜์–ด, interceptํ•  ์ˆ˜ ์žˆ๊ธฐ๋ฅผ ๋ฐ”๋ž€๋‹ค. ES6์—์„œ Reflect๋ผ๋Š” ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋˜์—ˆ๊ณ , ์ด๊ฒƒ์ด ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์ค€๋‹ค.

const dinner = {
  meal: 'tacos'
}

const handler = {
  get(target, property, receiver) {
    return Reflect.get(...arguments)
  }
}

const proxy = new Proxy(dinner, handler)
console.log(proxy.meal)

๐ŸŠ Reflect Object

Reflect๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ operation์„ ๊ฐ€๋กœ์ฑŒ ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋นŒํŠธ์ธ๊ฐ์ฒด์ด๋‹ค. proxy handler์™€ ๊ฐ™์€ ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

Description

  • Reflect๋Š” ์ผ๋ฐ˜์ ์ธ ๊ฐ์ฒด์ด๋‹ค.

  • ํ•จ์ˆ˜ ๊ฐ์ฒด๊ฐ€ ์•„๋‹ˆ๋‹ค => constructible ํ•˜์ง€ ์•Šใ„ด๋‹ค.

  • Reflect ๋‚ด๋ถ€์—๋Š” [[Construct]] method๊ฐ€ ๋‚ด๋ถ€์— ์—†๋‹ค => new operator๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ constructor๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

  • Reflect๋Š” [[Call]] internal method๊ฐ€ ์—†๋‹ค => ํ•จ์ˆ˜์ฒ˜๋Ÿผ ํ˜ธ์ถœ๋  ์ˆ˜ ์—†๋‹ค.

  • ๋ชจ๋“  Reflect์˜ ๋ฉ”์†Œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋Š” staticํ•˜๋‹ค.

์ถœ์ฒ˜

Last updated