Metaprogramming refers to manipulating or extending a program from within itself. ES2015 introduced the Proxy API for metaprogramming in JavaScript. A Proxy allows defining custom behavior for fundamental operations like property lookup, assignment, and enumeration. It works by wrapping a target object and defining traps (functions) for operations. While proxies enable powerful abstractions, they can impact performance due to the added layer of indirection. However, use cases like validation, logging, and virtual objects demonstrate how proxies can extend functionality transparently.
23. METAPROGRAMMING. HOW?
Reflection is key strategy to do metaprogramming:
Introspection - read the structure of a program
Self-modification - change program structure
Intercession - change the semantics of some language operations
66. VIRTUAL OBJECT
Virtual objects are proxies that emulate other objects
without those objects needing to be in the same address
space.
Use set, get, defineProperty, deleteProperty traps
#27: TheObject.seal()method seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable
TheObject.freeze()method freezes an object: that is, prevents new properties from being added to it; prevents existing properties from being removed; and prevents existing properties, or their enumerability, configurability, or writability, from being changed, it also prevents the prototype from being changed.
TheObject.preventExtensions()method prevents new properties from ever being added to an object (i.e. prevents future extensions to the object).