[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Meta_programming [Back]  [Original]

- JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Proxy Reflect (: , , , ) . JavaScript .

In this article

Proxy .

,

js
const handler = {
  get(target, name) {
    return name in target ? target[name] : 42;
  },
};

const p = new Proxy({}, handler);
p.a = 1;
console.log(p.a, p.b); // 1, 42

Proxy target( ) get handler . undefined 42 .

Proxy .

.

handler

.

traps

. ( '' .)

target

. . ( ) target .

invariants

'' . TypeError .

Proxy . .

/
handler.getPrototypeOf() Object.getPrototypeOf()
Reflect.getPrototypeOf()
__proto__
Object.prototype.isPrototypeOf()
instanceof
handler.setPrototypeOf() Object.setPrototypeOf()
Reflect.setPrototypeOf()
handler.isExtensible() Object.isExtensible()
Reflect.isExtensible()
handler.preventExtensions() Object.preventExtensions()
Reflect.preventExtensions()
handler.getOwnPropertyDescriptor() Object.getOwnPropertyDescriptor()
Reflect.getOwnPropertyDescriptor()
handler.defineProperty() Object.defineProperty()
Reflect.defineProperty()
handler.has()
foo in proxy
foo in Object.create(proxy)
Reflect.has()
handler.get()
proxy[foo]
proxy.bar
Object.create(proxy)[foo]
Reflect.get()
handler.set()
proxy[foo] = bar
proxy.foo = bar
Object.create(proxy)[foo] = bar
Reflect.set()
handler.deleteProperty()
delete proxy[foo]
delete proxy.foo
Reflect.deleteProperty()
handler.ownKeys() Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.keys()
Reflect.ownKeys()
handler.apply() proxy(..args)
Function.prototype.apply() Function.prototype.call()
Reflect.apply()
handler.construct() new proxy(...args)
Reflect.construct()

Proxy

Proxy.revocable() Proxy . revoke .

TypeError .

js
const revocable = Proxy.revocable(
  {},
  {
    get(target, name) {
      return `[[${name}]]`;
    },
  },
);
const proxy = revocable.proxy;
console.log(proxy.foo); // "[[foo]]"

revocable.revoke();

console.log(proxy.foo); // TypeError:   'get'   
proxy.foo = 1; // TypeError:   'set'   
delete proxy.foo; // TypeError:   'deleteProperty'   
console.log(typeof proxy); // "object", typeof   

(Reflection)

Reflect . .

Reflect .

Reflect target .

, Reflect.has() in :

js
Reflect.has(Object, "assign"); // true

apply()

Reflect , this ( ) arguments Function.prototype.apply() .

js
Function.prototype.apply.call(Math.floor, undefined, [1.75]);

Reflect.apply .

js
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"

Object.defineProperty , TypeError . try...catch . , Reflect.defineProperty() , if...else :

js
if (Reflect.defineProperty(target, property, attributes)) {
  // 
} else {
  // 
}

Web Proxy Viewer  |  New URL  |  Original Page