Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Basic JavaScript

JS Tutorial JS Syntax JS Variables JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Dates JS Arrays JS Typed Arrays JS Sets JS Maps JS Math JS RegExp JS Data Types JS Errors JS Events JS Programming JS References JS Versions

JS Advanced

JS Functions JS Objects JS Classes JS Iterations JS Asynchronous JS Modules JS Meta & Proxy JS HTML DOM JS Windows JS Web API JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JavaScript Meta Programming

Metaprogramming

Metaprogramming refers to a number of ways a program can manipulate itself:

  • Modify objects at runtime
  • Inspect objects at runtime
  • Control objects at runtime
  • Intercept running operations
  • Modify functions, and classes
  • Generate dynamic code

The Easy Explanation

Normally, code handles data.

With metaprogramming, code handles code.


Inspecting Objects

With the method Object.keys() you can inspect object properties.

Using Object.keys() is a simple example of metaprogramming.

Example

This code is analyzing another piece of code (an object):

// Create an Object
const user = {name: "Jan", age: 40};

// Fill Array with Object keys
const myArr = Object.keys(user);
Try it Yourself »

Modify Objects

I typical metaprogramming task is to modify object behaviour:

Example

// Create an Object
const person = {name: "John", age: 41};

// Define "name" to return "secret"
Object.defineProperty(person, "name", {
  get() { return "secret"; }
});

let name = person.name;
Try it Yourself »

Generate Dynamic Code

Metaprogramming involves dynamic code generation.

JavaScript can generate functions at runtime:

Example

const fn = new Function("a", "b", "return a + b");
Try it Yourself »

Metaprogramming Examples

ConseptDescription
ValidationRestrict the values that can be set to a property
LoggingDisplay property changes using a Proxy
DebuggingIntercept an operation using a Proxy
FrameworksVue, MobX, and Svelte use metaprogramming to detect state changes
ORM / database mappingWrap objects and creates fields based on database schema
Dynamic APIsCreate functions or object structures at runtime

Proxy Metaprogramming

The two objects Proxy and Reflect allow for programming at the meta level in JavaScript.

Proxy can be used to intercept property operations like reading or writing.

In the example below:

  • A user object is wrapped in a Proxy
  • The Proxy uses a set() trap to log whenever a property is set

Example

Log changes to property values:

// Create an Object
const user = {name: "Jan", age: 40};

// Wrap the Object in a Proxy
const proxy = new Proxy(user, {
  // Use a set trap
  set(target, property, value) {
    // Log changes
    log(property + "; " + value);
    return target[property];
  }
});

// Change Properties
proxy.name = "John";
proxy.age = 45;
proxy.name = "Paul";
Try it Yourself »

Proxy with Reflect

Reflect makes Proxy behavior match normal object behavior

In the example below:

  • A user object is wrapped in a Proxy
  • The Proxy uses a set() trap to log when a property is set
  • The set trap uses Reflect.set() for safe forwarding

Example

Log changes to property values:

// Create an Object
const user = {name: "Jan", age: 40};

// Wrap the Object in a Proxy
const proxy = new Proxy(user, {
  // Use a set trap
  set(target, property, value) {
    // Log changes
    log(property + ": " + value);
    // Safe forwarding with Reflect
    return Reflect.set(target, property, value);
  }
});
Try it Yourself »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.