Introduction
In programming, there are levels:
- At the base level (also called: application level), code processes user input.
- At the meta level, code processes base level code.
Examples
All Object.* methods can be considered metaprogramming functionality
Kinds of metaprogramming
Reflective metaprogramming means that a program processes itself. Kiczales et al2. [2] distinguish three kinds of reflective metaprogramming:
- Introspection: you have read-only access to the structure of a program.
- Self-modification: you can change that structure
- Intercession: you can define the semantics of some language operations
Introspection
Object.keys() performs introspection
self-modification
The following function moveProperty
moves a property from a source to
a target. It performs self-modification via the bracket operator for
property access, the assignment operator and the delete operator. (In
production code, you’d probably use property descriptors for this task.)
intercession
ECMAScript 5 doesn’t support intercession. Proxies were created to fill that gap.
Footnotes
Footnotes
-
Don’t ever use eval. If you think you’re a clever clogs that can work your away around the obvious security issues, then you especially should not use eval. ↩
-
“The Art of the Metaobject Protocol” by Gregor Kiczales, Jim des Rivieres and Daniel G. Bobrow. Book, 1991. ↩