--- title: 메타 프로그래밍 slug: Web/JavaScript/Guide/Meta_programming translation_of: Web/JavaScript/Guide/Meta_programming original_slug: Web/JavaScript/Guide/메타_프로그래밍 ---
Starting with ECMAScript 2015, JavaScript gains support for the {{jsxref("Proxy")}} and {{jsxref("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.
ECMAScript 6에서 소개되었습니다, {{jsxref("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 empty 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 {{jsxref("Proxy")}} reference page.
The following terms are used when talking about the functionality of proxies.
The following table summarizes the available traps available to Proxy objects. See the reference pages for detailed explanations and examples.
| Handler / trap | Interceptions | Invariants |
|---|---|---|
| {{jsxref("Global_Objects/Proxy/handler/getPrototypeOf", "handler.getPrototypeOf()")}} | {{jsxref("Object.getPrototypeOf()")}} {{jsxref("Reflect.getPrototypeOf()")}} {{jsxref("Object/proto", "__proto__")}} {{jsxref("Object.prototype.isPrototypeOf()")}} {{jsxref("Operators/instanceof", "instanceof")}} |
|
| {{jsxref("Global_Objects/Proxy/handler/setPrototypeOf", "handler.setPrototypeOf()")}} | {{jsxref("Object.setPrototypeOf()")}} {{jsxref("Reflect.setPrototypeOf()")}} |
If target is not extensible, the prototype parameter must be the same value as Object.getPrototypeOf(target). |
| {{jsxref("Global_Objects/Proxy/handler/isExtensible", "handler.isExtensible()")}} | {{jsxref("Object.isExtensible()")}} {{jsxref("Reflect.isExtensible()")}} |
Object.isExtensible(proxy) must return the same value as Object.isExtensible(target). |
| {{jsxref("Global_Objects/Proxy/handler/preventExtensions", "handler.preventExtensions()")}} | {{jsxref("Object.preventExtensions()")}} {{jsxref("Reflect.preventExtensions()")}} |
Object.preventExtensions(proxy) only returns true if Object.isExtensible(proxy) is false. |
| {{jsxref("Global_Objects/Proxy/handler/getOwnPropertyDescriptor", "handler.getOwnPropertyDescriptor()")}} | {{jsxref("Object.getOwnPropertyDescriptor()")}} {{jsxref("Reflect.getOwnPropertyDescriptor()")}} |
|
| {{jsxref("Global_Objects/Proxy/handler/defineProperty", "handler.defineProperty()")}} | {{jsxref("Object.defineProperty()")}} {{jsxref("Reflect.defineProperty()")}} |
|
| {{jsxref("Global_Objects/Proxy/handler/has", "handler.has()")}} | Property query: foo in proxyInherited property query: foo in Object.create(proxy){{jsxref("Reflect.has()")}} |
|
| {{jsxref("Global_Objects/Proxy/handler/get", "handler.get()")}} | Property access: proxy[foo]and proxy.barInherited property access: Object.create(proxy)[foo]{{jsxref("Reflect.get()")}} |
|
| {{jsxref("Global_Objects/Proxy/handler/set", "handler.set()")}} | Property assignment: proxy[foo] = barand proxy.foo = barInherited property assignment: Object.create(proxy)[foo] = bar{{jsxref("Reflect.set()")}} |
|
| {{jsxref("Global_Objects/Proxy/handler/deleteProperty", "handler.deleteProperty()")}} | Property deletion: delete proxy[foo] and delete proxy.foo{{jsxref("Reflect.deleteProperty()")}} |
A property cannot be deleted, if it exists as a non-configurable own property of the target object. |
| {{jsxref("Global_Objects/Proxy/handler/enumerate", "handler.enumerate()")}} | Property enumeration / for...in: for (var name in proxy) {...}{{jsxref("Reflect.enumerate()")}} |
The enumerate method must return an object. |
| {{jsxref("Global_Objects/Proxy/handler/ownKeys", "handler.ownKeys()")}} | {{jsxref("Object.getOwnPropertyNames()")}} {{jsxref("Object.getOwnPropertySymbols()")}} {{jsxref("Object.keys()")}} {{jsxref("Reflect.ownKeys()")}} |
|
| {{jsxref("Global_Objects/Proxy/handler/apply", "handler.apply()")}} | proxy(..args){{jsxref("Function.prototype.apply()")}} and {{jsxref("Function.prototype.call()")}} {{jsxref("Reflect.apply()")}} |
There are no invariants for the handler.applymethod. |
| {{jsxref("Global_Objects/Proxy/handler/construct", "handler.construct()")}} | new proxy(...args){{jsxref("Reflect.construct()")}} |
The result must be an Object. |
ProxyThe {{jsxref("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 on the proxy leads to a {{jsxref("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
{{jsxref("Reflect")}} is a built-in object that provides methods for interceptable JavaScript operations. The methods are the same as those of the {{jsxref("Global_Objects/Proxy/handler","proxy handlers","","true")}}. Reflect is not a function object.
Reflect helps with forwarding default operations from the handler to the target.
With {{jsxref("Reflect.has()")}} for example, you get the in operator as a function:
Reflect.has(Object, 'assign'); // true
apply functionIn ES5, you typically use the {{jsxref("Function.prototype.apply()")}} method to call a function with a given this value and arguments provided as an array (or an array-like object).
Function.prototype.apply.call(Math.floor, undefined, [1.75]);
With {{jsxref("Reflect.apply")}} this becomes less verbose and easier to understand:
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"
With {{jsxref("Object.defineProperty")}}, which returns an object if successful, or throws a {{jsxref("TypeError")}} otherwise, you would use a {{jsxref("Statements/try...catch","try...catch")}} block to catch any error that occurred while defining a property. Because {{jsxref("Reflect.defineProperty")}} returns a Boolean success status, you can just use an {{jsxref("Statements/if...else","if...else")}} block here:
if (Reflect.defineProperty(target, property, attributes)) {
// success
} else {
// failure
}
{{Previous("Web/JavaScript/Guide/Iterators_and_Generators")}}