MDN’s new design is in Beta! A sneak peek: https://blog.mozilla.org/opendesign/mdns-new-design-beta/

메타 프로그래밍

현재 번역은 완벽하지 않습니다. 한국어로 문서 번역에 동참해주세요.

Starting with ECMAScript 6, JavaScript gains support for the Proxy and Reflect objects allowing you to intercept and define custom behavior for fundamental language operations (e.g. property lookup, assignment, enumeration, function invocation, etc). With the help of these two objects you are able to program at the meta level of JavaScript.

Proxies

 ECMAScript 6에서 소개되었습니다, Proxy 객체는  특정 작업을 가로막는것과  사용자 정의 행위를 시행하는것을 허용합니다.예를 들면 객체가 속성을 가지는 것입니다:

var handler = {
  get: function(target, name){
    return name in target ? target[name] : 42;
}};
var p = new Proxy({}, handler);
p.a = 1;
console.log(p.a, p.b); // 1, 42

The Proxy object defines a target (an object here) and a handler object in which a get trap is implemented. Here, an object that is proxied will not return undefined when getting undefined properties, but will instead return the number 42.

Additional examples are available on the Proxy reference page.

Terminology

The following terms are used when talking about the functionality of proxies.

handler
Placeholder object which contains traps.
traps
The methods that provide property access. This is analogous to the concept of traps in operating systems.
target
Object which the proxy virtualizes. It is often used as storage backend for the proxy. Invariants (semantics that remain unchanged) regarding object non-extensibility or non-configurable properties are verified against the target.
invariants
Semantics that remain unchanged when implementing custom operations are called invariants. If you violate the invariants of a handler, a TypeError will be thrown.

Handlers and traps

The following table summarizes the available traps available to Proxy objects. See the reference pages for detailed explanations and examples.

Handler / trap Interceptions Invariants
handler.getPrototypeOf() Object.getPrototypeOf()
Reflect.getPrototypeOf()
__proto__
Object.prototype.isPrototypeOf()
instanceof
getPrototypeOf method must return an object or null.

If target is not extensible, Object.getPrototypeOf(proxy) method must return the same value as Object.getPrototypeOf(target).
handler.setPrototypeOf() Object.setPrototypeOf()
Reflect.setPrototypeOf()

If target is not extensible, the prototype parameter must be the same value as Object.getPrototypeOf(target).

handler.isExtensible()

Object.isExtensible()

Reflect.isExtensible()

Object.isExtensible(proxy) must return the same value as Object.isExtensible(target).

handler.preventExtensions()

Object.preventExtensions()

Reflect.preventExtensions()

Object.preventExtensions(proxy) only returns true if Object.isExtensible(proxy) is false.

handler.getOwnPropertyDescriptor()

Object.getOwnPropertyDescriptor()

Reflect.getOwnPropertyDescriptor()

getOwnPropertyDescriptor must return an object or undefined.

A property cannot be reported as non-existent, if it exists as a non-configurable own property of the target object.

A property cannot be reported as non-existent, if it exists as an own property of the target object and the target object is not extensible.

A property cannot be reported as existent, if it does not exists as an own property of the target object and the target object is not extensible.

A property cannot be reported as non-configurable, if it does not exists as an own property of the target object or if it exists as a configurable own property of the target object.

The result of Object.getOwnPropertyDescriptor(target) can be applied to the target object using Object.defineProperty and will not throw an exception.

handler.defineProperty()

Object.defineProperty()

Reflect.defineProperty()

A property cannot be added, if the target object is not extensible.

A property cannot be added as or modified to be non-configurable, if it does not exists as a non-configurable own property of the target object.

A property may not be non-configurable, if a corresponding configurable property of the target object exists.

If a property has a corresponding target object property then Object.defineProperty(target, prop, descriptor) will not throw an exception.

In strict mode, a false return value from the defineProperty handler will throw a TypeError exception.

handler.has()

Property query: foo in proxy

Inherited property query: foo in Object.create(proxy)

Reflect.has()

A property cannot be reported as non-existent, if it exists as a non-configurable own property of the target object.

A property cannot be reported as non-existent, if it exists as an own property of the target object and the target object is not extensible.

handler.get()

Property access: proxy[foo]and proxy.bar

Inherited property access: Object.create(proxy)[foo]

Reflect.get()

The value reported for a property must be the same as the value of the corresponding target object property if the target object property is a non-writable, non-configurable data property.

The value reported for a property must be undefined if the corresponding target object property is non-configurable accessor property that has undefined as its [[Get]] attribute.

handler.set()

Property assignment: proxy[foo] = bar and proxy.foo = bar

Inherited property assignment: Object.create(proxy)[foo] = bar

Reflect.set()

Cannot change the value of a property to be different from the value of the corresponding target object property if the corresponding target object property is a non-writable, non-configurable data property.

Cannot set the value of a property if the corresponding target object property is a non-configurable accessor property that has undefined as its [[Set]] attribute.

In strict mode, a false return value from the set handler will throw a TypeError exception.

handler.deleteProperty()

Property deletion: delete proxy[foo] and delete proxy.foo

Reflect.deleteProperty()

A property cannot be deleted, if it exists as a non-configurable own property of the target object.
handler.enumerate()

Property enumeration / for...in: for (var name in proxy) {...}

Reflect.enumerate()

The enumerate method must return an object.
handler.ownKeys()

Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.keys()
Reflect.ownKeys()

The result of ownKeys is a List.

The Type of each result List element is either String or Symbol.

The result List must contain the keys of all non-configurable own properties of the target object.

If the target object is not extensible, then the result List must contain all the keys of the own properties of the target object and no other values.

handler.apply()

proxy(..args)

Function.prototype.apply() and Function.prototype.call()

Reflect.apply()

There are no invariants for the handler.apply method.
handler.construct()

new proxy(...args)
Reflect.construct()

The result must be an Object.

Revocable Proxy

The Proxy.revocable() method is used to create a revocable Proxy object. This means that the proxy can be revoked via the function revoke and switches the proxy off. Afterwards, any operation leads on the proxy leads to a TypeError.

var revocable = Proxy.revocable({}, {
  get: function(target, name) {
    return "[[" + name + "]]";
  }
});
var proxy = revocable.proxy;
console.log(proxy.foo); // "[[foo]]"

revocable.revoke();

console.log(proxy.foo); // TypeError is thrown
proxy.foo = 1           // TypeError again
delete proxy.foo;       // still TypeError
typeof proxy            // "object", typeof doesn't trigger any trap

Reflection

Reflect is a built-in object that provides methods for interceptable JavaScript operations. The methods are the same as those of the proxy handlers. Reflect It is not a function object.

Reflect helps with forwarding default operations from the handler to the target. Note that Reflect is not implemented in Firefox yet.

With Reflect.has() for example, you get the in operator as a function:

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

문서 태그 및 공헌자

 이 페이지의 공헌자: nodejs, stephenjang
 최종 변경: nodejs,