JavaScript Interview Question #189
What is metaprogramming in JavaScript?
Advanced JavaScript, Practical Problems & Senior Interview Scenarios Senior Advanced
Quick Interview Answer
Metaprogramming is code that inspects, modifies, or controls the behavior or structure of other code and objects. JavaScript supports metaprogramming through features such as property descriptors, symbols, Proxy, Reflect, and dynamic property access.
Detailed Explanation
Metaprogramming is code that inspects, modifies, or controls the behavior or structure of other code and objects. JavaScript supports metaprogramming through features such as property descriptors, symbols, Proxy, Reflect, and dynamic property access.
It is useful for frameworks, serialization, validation, dependency systems, and tooling, but excessive metaprogramming can make application behavior difficult to trace and debug.
Code Example
const product = {
id: 1,
name: "Keyboard"
};
for (const key of Reflect.ownKeys(product)) {
console.log(
key,
Reflect.get(product, key)
);
}