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