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)๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

Proxy ( target, handler )

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

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

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

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

handler.construct()

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

handler.apply()

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

์˜ˆ์ œ

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

๐ŸŠ Reflect Object

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

Description

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

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

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

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

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

์ถœ์ฒ˜

Last updated