# 17. Proxy, JS interception 기능 구현!

😌 신기하다... interception 기능이 되다니!

# ✨ Proxy, Interception

Reflect.get(target,property)target[property] 와 같다.

proxy 내에서는 Reflect를 쓰는 걸 추천한다.

const proxy = new Proxy({name: 'crong', changedValue: 0}, {
  get: function(target, property, receiver) {
    console.log('get value');
    return (property in target) ? Reflect.get(target,property) : 'anonymous'; 

  },
  set: function(target, property, value) {
    console.log('set value', value);
    target['changedValue']++;
    target[property] = value;
  }
});

proxy.name="codesquad";
console.log(proxy.changedValue);

console.log('-----');

proxy.name="codesquad2";
console.log(proxy.changedValue);

# 🔎 console


"set value"
"codesquad"
"get value"
1
"-----"
"set value"
"codesquad2"
"get value"
2
"-----"
"get value"
"anonymous"

# Reference


https://www.inflearn.com/course/es6-강좌-자바스크립트/dashboard (opens new window)

https://jsbin.com/ (opens new window)

Last Updated: 3/8/2024, 5:46:31 AM