Loading lesson path
Concept visual
Start at both ends
Metaprogramming refers to a number of ways a program can manipulate itself
Modify functions, and classes
Normally, code handles data. With metaprogramming, code handles code.
Object.keys() you can inspect object properties.
Object.keys() is a simple example of metaprogramming.
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);I typical metaprogramming task is to modify object behaviour
// 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;Metaprogramming involves dynamic code generation. JavaScript can generate functions at runtime:
const fn = new Function("a", "b", "return a + b");Restrict the values that can be set to a property
Vue, MobX, and Svelte use metaprogramming to detect state changes
Formula
ORM / database mappingWrap objects and creates fields based on database schema