JavaScript Meta-programming with Proxies and Reflection

Héla Ben Khalfallah
ekino-france
Published in
15 min readMay 31, 2024

Overview, applications, best practices, and limitations

Photo by Nathan da Silva on Unsplash

Introduction

Upon reading the title, you might wonder: What is meta-programming? How does it differ from regular programming? What are its key concepts and techniques? What are some practical examples? How do various languages implement it? What are its pros and cons? And how can it be used in JavaScript?

That’s a lot of questions! But don’t worry, in this article, I’ll aim to satisfy our curiosity and answer each one. Here is the outline I propose:
· First Footprint on the Planet
What is meta-programming?
Meta-programming in JavaScript
· Hands-on Proxies and Reflect API
Using Proxy and Reflect to intercept property access (get)
Using Proxy and Reflect to intercept property assignment (set)
Can we use a random key instead of get and set?
Practical Examples of Proxy and Reflect
· Case studies and real-world examples
Creating a reactive store
Creating a library for validation and sanitization
Building a secure API Gateway
Real-World frameworks and libraries using Proxy and Reflect
· Best practices, common errors, and recommendations
Best practices
Common errors and recommendations
· Conclusion

If you’re eager and curious to delve into this concept, I invite you to join me on this journey. Buckle up; we’re about to take off! 🚀

First Footprint on the Planet

What is meta-programming?

To find a simple definition of meta-programming, let’s start by examining examples from various programming languages:

#define PI 3.14159
#define SQUARE(x) ((x) * (x))

The code above is commonly found in languages like C and C++. It is a macro preprocessor, used to define constants, functions, and perform conditional compilation.

The #define directive creates macros, which are placeholders for code that can be reused throughout the source code:

int main() {
int radius = 5;
double area = PI * SQUARE(radius); // placeholders
printf("Area: %f\n", area); // Output: Area: 78.539750
return 0;
}

When the preprocessor encounters a macro in the source code, it replaces the macro with its defined content:

// Before preprocessing:
#define PI 3.14159
#define SQUARE(x) ((x) * (x))

int main() {
int radius = 5;
double area = PI * SQUARE(radius);
printf("Area: %f\n", area);
return 0;
}

// After preprocessing:
int main() {
int radius = 5;
double area = 3.14159 * ((5) * (5));
printf("Area: %f\n", area);
return 0;
}

The preprocessor can include or exclude parts of the code based on certain conditions, using directives like #if, #ifdef, #ifndef, #else, and #endif.

#define DEBUG

#ifdef DEBUG
printf("Debug mode\n");
#endif

After preprocessing, the expanded code is compiled into machine code by the compiler.

The power of macros lies in their ability to provide powerful tools for code reusability, conditional compilation, code generation, and transformation.

Did you know that macros are a form of meta-programming? Here’s an initial definition of meta-programming:

Metaprogramming is a programming technique in which computer programs have the ability to treat other programs as their data. It means that a program can be designed to read, generate, analyse or transform other programs, and even modify itself while running. — https://en.wikipedia.org/wiki/Metaprogramming

🚩 It’s important to note that compilers and metaprogramming are distinct concepts. While both involve manipulating code, their goals and methods differ:

  • Metaprogramming focuses on generating or modifying code, often at runtime, to automate tasks and create higher-level abstractions.
  • In contrast, compiler theory is concerned with translating code from one form to another, typically converting high-level source code into machine code or byte-code for execution.

Rust also has Macro:

macro_rules! say_hello {
() => {
println!("Hello, world!");
};
}

fn main() {
say_hello!(); // Expands to: println!("Hello, world!");
}

Macro, as we know, allows us to write code that generates or transforms other code during compilation.

Fundamentally, macros are a way of writing code that writes other code, which is known as metaprogramming. […] Metaprogramming is useful for reducing the amount of code you have to write and maintain, which is also one of the roles of functions. However, macros have some additional powers that functions don’t. — https://doc.rust-lang.org/book/ch19-06-macros.html

In metaprogramming, macros are just one of many tools and techniques that are used. Metaprogramming encompasses a variety of methods that allow programs to generate, manipulate, or transform other programs. Here are some key techniques besides macros:

1️⃣ Annotations and Attributes ( Java ): annotations in Java provide a powerful way to add metadata to code, which can be processed at compile-time or runtime to enforce certain behaviors, configurations, or constraints.

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

// Custom annotation with compile-time retention
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
@interface MyAnnotation {
String value();
}

// Custom annotation with runtime retention
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MyRuntimeAnnotation {
String value();
}

2️⃣ Template Metaprogramming ( C++ ): Template metaprogramming is a powerful technique that leverages the compiler to generate efficient and type-safe code, providing significant benefits in terms of performance and maintainability.

#include <iostream>

// Primary template
template<int N>
struct Factorial {
static const int value = N * Factorial<N - 1>::value;
};

// Template specialization for base case
template<>
struct Factorial<0> {
static const int value = 1;
};

int main() {
// Calculate factorial of 5 at compile-time
std::cout << "Factorial of 5 is " << Factorial<5>::value << std::endl;
return 0;
}

3️⃣ Reflection ( Java ): Reflection offers a powerful mechanism for dynamically interacting with code, enabling us to inspect classes, methods, fields, and more, as well as to invoke methods or access fields.

import java.lang.reflect.Method;

// Define a simple class with a method to be called using reflection
public class ReflectExample {
public void sayHello(String name) {
System.out.println("Hello, " + name + "!");
}

public static void main(String[] args) {
try {
// Create an instance of the class
ReflectExample example = new ReflectExample();

// Get the Class object associated with ReflectExample
Class<?> clazz = example.getClass();

// Get the Method object representing the sayHello method
Method method = clazz.getMethod("sayHello", String.class);

// Invoke the sayHello method on the example instance with the argument "World"
method.invoke(example, "World");
} catch (Exception e) {
e.printStackTrace();
}
}
}

4️⃣ Dynamic Evaluation ( JavaScript ):

const code = 'console.log("Hello, World!");';
eval(code); // Output: Hello, World!

⚠️ Be careful, it’s not recommended to use eval in JavaScript.

In summary, metaprogramming is a programming paradigm where programs have the ability to treat other programs as data. This typically involves:

  • Code Manipulation: the ability to generate, transform, or inspect code dynamically.
  • Automation: automating repetitive or complex tasks by manipulating code.
  • Abstraction: creating higher-level abstractions to simplify and reduce redundancy in code.
  • Dynamic Behavior: adapting the program’s behavior at runtime based on different conditions or inputs.

I think your question now is how to apply metaprogramming in the context of JavaScript beyond using eval. Let’s see!

Meta-programming in JavaScript

JavaScript has several powerful tools that can be used for metaprogramming, such as Proxies, the Reflect API, and Decorators.

Oh, don’t worry! I’m talking about JavaScript, not Java. 😊

Each of these techniques serves different purposes and offers different functionalities:

  • Proxy: offers high flexibility by intercepting various operations on objects, useful for dynamic behavior, validation, logging, etc.
  • Reflect: provides a consistent and simpler API for performing common object operations, often used alongside proxies to streamline the implementation of traps.
  • Decorators (Proposed): allow for declarative modification of classes and methods, enhancing or altering their behavior through annotations.

You might be wondering: How do JavaScript proxies fit into the concept of metaprogramming? Well, JavaScript proxies align with the principles of metaprogramming by enabling dynamic code manipulation, automating tasks, creating abstractions, and adapting behavior at runtime.

By intercepting and customizing fundamental operations on objects, proxies provide a powerful way to enhance flexibility, reduce redundancy, and manage complexity in code, fulfilling the core goals of metaprogramming.

After this somewhat theoretical introduction to the key concepts of metaprogramming, it’s time to dive into practical examples with JavaScript proxies. Let’s get started! 💻

Hands-on Proxies and Reflect API

Using Proxy and Reflect to intercept property access (get)

As we saw earlier, a proxy in JavaScript allows for the intercepting and redefinition of fundamental operations for an object, such as property access, assignment, enumeration, and function invocation.

Here’s a basic way to create a proxy to intercept property access:

https://gist.github.com/helabenkhalfallah/3be08466a32075b29c745ec515d9c90c

You can play with the code here.

✳️ target is a simple object with a single property message that contains the string "Hello, World!".

✳️ handler is an object that defines a get trap. The get trap is a method that intercepts property access on the target object.

✳️ When a property on the proxy is accessed, the get trap is triggered, logging a message indicating which property was accessed.

✳️ The Reflect.get method is used to perform the default behavior of retrieving the property value from the target object. Reflect.get is part of the Reflect API, which provides a set of methods for performing common object operations in a consistent and standard way.

Using Proxy and Reflect to intercept property assignment (set)

Here’s a basic way to create a proxy to intercept property set:

https://gist.github.com/helabenkhalfallah/60053e204c2b858cf40dae10f83f36bc

You can play with the code here.

✳️ The handler object defines a set trap. The set trap is a function that intercepts property assignment on the target object.

✳️ When a property on the proxy is assigned a value, the set trap is triggered.

✳️ Inside the set trap:

  • logs the message indicating which property is being set and to what value.
  • target[prop] = value; performs the actual assignment of the value to the property on the target object.
  • return true; indicates that the assignment was successful. Returning true is important as it signals to the proxy that the operation was handled correctly.

Can we use a random key instead of get and set?

In the context of JavaScript proxies, the keys get and set are specific traps provided by the Proxy API to intercept property access and assignment operations. These keys are predefined and cannot be replaced with arbitrary keys.

However, there are other predefined traps we can use to intercept different types of operations. Here’s a list of all the traps available in the Proxy API:

Proxy API (Image bu the author)

The has trap intercepts the in operator:

https://gist.github.com/helabenkhalfallah/5ad2e8f49a3e9879b9761c87cd2f51bb

The deleteProperty trap intercepts the delete operator:

https://gist.github.com/helabenkhalfallah/677240209d26935c42876e992bfba749

The apply trap intercepts function calls:

https://gist.github.com/helabenkhalfallah/b77267a473c45404684ef10bc85b2f0b

The ownKeys trap intercepts operations like Object.getOwnPropertyNames and Object.keys :

https://gist.github.com/helabenkhalfallah/a253aa2476ed9fd81f3f8ff543b0ffce

Here’s a combined example of multiple traps in one proxy:

https://gist.github.com/helabenkhalfallah/c6b2f1da0b9e864993f296abb8631977

You can play with the code here. That’s awesome!

The Proxy API and Reflect API complement each other. The Proxy API provides traps that intercept various operations, allowing us to define custom behavior, while the Reflect API provides a set of methods that mirror these traps, making it easy to perform default behavior within the traps:

const target = {
name: 'Alice',
age: 30
};

const handler = {
get: function(target, prop, receiver) {
console.log(`Getting property ${prop}`);
// Use Reflect to perform the default behavior
return Reflect.get(target, prop, receiver);
},
set: function(target, prop, value, receiver) {
console.log(`Setting property ${prop} to ${value}`);
// Use Reflect to perform the default behavior
return Reflect.set(target, prop, value, receiver);
},
has: function(target, prop) {
console.log(`Checking if property ${prop} is in target`);
// Use Reflect to perform the default behavior
return Reflect.has(target, prop);
},
deleteProperty: function(target, prop) {
console.log(`Deleting property ${prop}`);
// Use Reflect to perform the default behavior
return Reflect.deleteProperty(target, prop);
},
....

In other words, the Reflect API helps to restore default object behavior when using proxies. By using Reflect methods within proxy traps, we ensure that our custom logic can coexist with JavaScript’s standard behavior, leading to more predictable and reliable code.

If we don’t use the Reflect API within the proxy traps, we will need to manually handle the default behavior of the intercepted operations. This can be error-prone and may not always achieve the same consistency and reliability as using Reflect.

Here’s what can happen in the get case, for example:

const handler = {
get: function(target, prop, receiver) {
console.log(`Getting property ${prop}`);
return target[prop]; // Manually accessing the property
}
};

const proxy = new Proxy(target, handler);
console.log(proxy.name); // Logs: Getting property name. Output: Alice

Potential Issues: directly accessing target[prop] might not correctly handle all cases, such as inherited properties or getters.

const parent = {
inheritedProp: "I'm inherited"
};

const target = {
...parent,
ownProp: "I'm own property",
};


const handler = {
get: function(target, prop, receiver) {
console.log(`Getting property ${prop}`);
return target[prop]; // Directly accessing the property
}
};

const proxy = new Proxy(target, handler);

// Logs: Getting property ownProp. Output: I'm own property
console.log(proxy.ownProp);

// Logs: Getting property inheritedProp. Output: nothing
console.log(proxy.inheritedProp);

The inheritedProp from the prototype chain is not taken into account by target[prop].

However using Reflect.get in the get trap ensures that both inherited properties and properties with getters are handled correctly:

const parent = {
inheritedProp: "I'm inherited"
};

const target = {
...parent,
ownProp: "I'm own property",
};


const handler = {
get: function(target, prop, receiver) {
console.log(`Getting property ${prop}`);
return Reflect.get(target, prop, receiver);
}
};

const proxy = new Proxy(target, handler);

// Logs: Getting property ownProp. Output: I'm own property
console.log(proxy.ownProp);

// Logs: Getting property inheritedProp. Output: I'm inherited
console.log(proxy.inheritedProp);

💡Using Reflect ensures that the operations behave as they would normally in JavaScript, preserving the language’s built-in behaviors and handling edge cases that you might miss when manually implementing these operations.

Now that we understand how Proxy and Reflect work, let’s explore practical examples where using Proxy is recommended.

Practical Examples of Proxy and Reflect

✴️ Example 1: Logging Property Access and Modification

For debugging and monitoring object interactions, logging property access and modifications using Proxy and Reflect can be helpful:

const target = {
name: 'Alice',
age: 30
};

const handler = {
get: function(target, prop, receiver) {
console.log(`Getting property ${prop}`);
return Reflect.get(target, prop, receiver);
},
set: function(target, prop, value, receiver) {
console.log(`Setting property ${prop} to ${value}`);
return Reflect.set(target, prop, value, receiver);
}
};

const proxy = new Proxy(target, handler);

console.log(proxy.name); // Logs: Getting property name. Output: Alice
proxy.age = 31; // Logs: Setting property age to 31
console.log(proxy.age); // Logs: Getting property age. Output: 31

✴️ Example 2: Validation

Using a proxy to enforce validation rules before setting property values:

const target = {
age: 25
};

const handler = {
set: function(target, prop, value, receiver) {
if (prop === 'age' && (typeof value !== 'number' || value <= 0)) {
throw new TypeError('Age must be a positive number');
}
console.log(`Setting property ${prop} to ${value}`);
return Reflect.set(target, prop, value, receiver);
}
};

const proxy = new Proxy(target, handler);

proxy.age = 30; // Logs: Setting property age to 30
console.log(proxy.age); // Output: 30
// proxy.age = -5; // Throws: TypeError: Age must be a positive number

✴️ Example 3: Dynamic Property Creation

Using Proxy to dynamically create properties and gracefully handle non-existent properties:

const target = {};

const handler = {
get: function(target, prop, receiver) {
if (!(prop in target)) {
target[prop] = `Property ${prop} did not exist, created dynamically`;
}
console.log(`Getting property ${prop}`);
return Reflect.get(target, prop, receiver);
}
};

const proxy = new Proxy(target, handler);

// Logs: Getting property name. Output: Property name did not exist, created dynamically
console.log(proxy.name);

// Logs: Getting property age. Output: Property age did not exist, created dynamically
console.log(proxy.age);

✴️ Example 4: Function Tracing

Using a proxy to trace function calls and log arguments:

const targetFunction = function(a, b) {
return a + b;
};

const handler = {
apply: function(target, thisArg, argumentsList) {
console.log(`Called with arguments: ${argumentsList}`);
return Reflect.apply(target, thisArg, argumentsList);
}
};

const proxy = new Proxy(targetFunction, handler);

console.log(proxy(1, 2)); // Logs: Called with arguments: 1,2. Output: 3
console.log(proxy(5, 10)); // Logs: Called with arguments: 5,10. Output: 15

✴️ Example 5: Property Deletion Logging

Using Proxy to log deletion of properties:

const target = {
name: 'Alice',
age: 30
};

const handler = {
deleteProperty: function(target, prop) {
console.log(`Deleting property ${prop}`);
return Reflect.deleteProperty(target, prop);
}
};

const proxy = new Proxy(target, handler);

delete proxy.age; // Logs: Deleting property age
console.log(target.age); // Output: undefined

💡 Obviously, in production mode, we replace the console statements with back-end calls to trace and save the logs.

The beauty of these examples lies in their seamless integration with existing code, making them very useful for developing libraries and frameworks, let’s see!

Case studies and real-world examples

Creating a reactive store

https://gist.github.com/helabenkhalfallah/b0fbcd2b842e9a06ad2fc0562049cf89

An example of using this store:

// Usage example
const store = createStore({ count: 0 });

// Subscribe to state changes
store.subscribe(state => {
console.log("State changed:", state);
});

// Accessing properties through the getter
console.log(store.getState().count); // Output: 0

// Updating the state through the setter
store.getState().count = 10; // Output: "State changed: { count: 10 }"

// Accessing properties through the getter
console.log(store.getState().count); // Output: 10

You can play with the code here.

This store can be used in a React functional component as well:

const store = createStore({ count: 0 });

// Function component to render UI based on state
function Counter() {
// State to hold the current state of the store
const [state, setState] = useState(store.getState());

// Effect to subscribe to state changes when the component mounts
useEffect(() => {
// Subscribe to state changes
const unsubscribe = store.subscribe((newState) => {
// Update the local state with the new state from the store
setState({ ...newState });
});

// Return the unsubscribe function to clean up subscriptions
return () => {
unsubscribe();
};
}, []);

// Effect to increment the count every 5 seconds
useEffect(() => {
// Set up a timer to increment the count every 5 seconds
const interval = setInterval(() => {
// Update the count in the store's state
store.getState().count += 1;
}, 1000);

// Clear the interval timer when the component unmounts
return () => {
clearInterval(interval);
};
}, []);

// Render the count from the local state
return <div>Count: {state.count}</div>;
}

export default Counter;

You can play with the code here.

Creating a library for validation and sanitization

Using the set trap to validate inputs before they are assigned to properties, preventing invalid data from entering the system:

https://gist.github.com/helabenkhalfallah/0977966aec44ac9c7cdc2424493484f8

💡Ensuring that data conforms to expected formats and constraints, enhancing security and consistency.

Building a secure API Gateway

Using JavaScript’s Proxy and Reflect APIs, we can create a secure, flexible, and maintainable API gateway:

https://gist.github.com/helabenkhalfallah/01406c844e35c7cf2ae3fc4e4e9ed151

You can play with the code here. This example brings to mind express and routing.

Now, let’s move on to industrial applications, i.e. focusing on real-world frameworks and libraries. 💫

Real-World frameworks and libraries using Proxy and Reflect

✳️ Vue 3: In Vue 3, the reactivity system is built around proxies to intercept and manage state changes, providing a seamless and performant way to handle reactivity:

✳️ MobX: by default, MobX uses proxies to make arrays and plain objects observable:

✳️ In Svelte, the reactivity system tracks dependencies and updates the DOM efficiently when state changes. This is achieved through the use of Proxy objects to intercept and react to state mutations.

These real-world examples highlight the significant capabilities and versatility that proxies and the Reflect API bring to modern JavaScript development.

Now that we’ve explored proxies through practical examples and real-world applications, it’s crucial to consider best practices for their implementation and be aware of common pitfalls to avoid for a complete understanding.🌟

Best practices, common errors, and recommendations

Best practices

🔵 Use the Reflect API within proxy traps to maintain consistent default behavior:

const handler = {
get(target, prop, receiver) {
console.log(`Getting property ${prop}`);
return Reflect.get(target, prop, receiver);
},
set(target, prop, value, receiver) {
console.log(`Setting property ${prop} to ${value}`);
return Reflect.set(target, prop, value, receiver);
}
};
const proxy = new Proxy({}, handler);

🔵 Apply proxies in scenarios like state management, logging, validation, or reactive programming. Proxies can introduce overhead, so they should be used where their benefits are clear.

🔵 Use the set trap to validate data before it is assigned:

const handler = {
set(target, prop, value, receiver) {
if (prop === 'age' && (typeof value !== 'number' || value < 0)) {
throw new TypeError('Age must be a positive number');
}
return Reflect.set(target, prop, value, receiver);
}
};
const proxy = new Proxy({}, handler);
proxy.age = 30; // Works fine
// proxy.age = -1; // Throws error

🔵 Use proxies to log operations for debugging purposes, but disable or remove such logging in production or use a back-end API.

🔵 Design proxy handlers to avoid self-referential loops that can cause infinite recursion:

const handler = {
get(target, prop, receiver) {
if (prop === 'self') return receiver;
return Reflect.get(target, prop, receiver);
}
};
const proxy = new Proxy({}, handler);

Let’s move onto the mistakes we should avoid. 🚫

Common errors and recommendations

🔴 Error: Proxies can introduce significant performance overhead, especially in performance-critical paths.

Recommendation: Measure performance impact and use proxies judiciously. Consider alternatives if performance is critical.

🔴 Error: Proxies can make debugging more complex because the behavior is abstracted and intercepted.

Recommendation: Use clear and concise logging within proxy traps and ensure thorough testing to understand proxy behavior.

🔴 Error: Proxies can expose sensitive data or allow unauthorized modifications.

Recommendation: Implement thorough validation and access control within proxy traps.

🔴 Error: Not all JavaScript environments fully support proxies, especially older browsers.

Recommendation: Ensure that the target environment supports proxies or provide fallback mechanisms.

By using proxies and the Reflect API in JavaScript, code can be significantly more flexible and powerful, allowing for dynamic behavior and advanced metaprogramming capabilities. In order to reap maximum benefits and prevent potential pitfalls, it is crucial to follow best practices and be aware of common errors. 🎯

Conclusion

In this article, we explored the powerful capabilities of JavaScript meta-programming, focusing on proxies and the Reflect API. We examined their concepts, best practices, common pitfalls, and real-world applications.

Proxies and the Reflect API enable dynamic behaviors like logging, validation, and fine-grained reactivity by intercepting object operations.

Best practices include using Reflect to maintain default behavior and implementing robust security checks, while common pitfalls involve performance overhead and debugging complexity.

Frameworks such as Vue.js and MobX utilize proxies for state management and reactivity. The shift toward reactive programming underscores the importance of building responsive and efficient applications.

This trend is likely to continue as the demand for real-time, interactive applications grows, and as technologies like WebAssembly further enhance the capabilities of web development.

Until we meet again in a new article and a fresh adventure! ❤️

Thank you for reading my article.

Want to Connect? 
You can find me at GitHub: https://github.com/helabenkhalfallah

--

--

Héla Ben Khalfallah
ekino-france

Hello! I'm Héla Ben Khalfallah. I'm a software engineer with a wealth of experience in web solutions, architecture, frontend, FrontendOps, and leadership.