JavaScript metaprogramming: Proxy and Reflect

Subscribe to my newsletter and never miss my upcoming articles

What is metaprogramming

Metaprogramming refers to the writing of certain types of computer programs that write or manipulate other programs (or themselves) as their data, or perform part of the work that should have been done at compile time at runtime. In many cases, meta-programming is more efficient than writing all the code manually. The language in which the metaprogram is written is called the metalanguage, and the language that is manipulated is called the target language. The ability of a language to be its own metalanguage is called reflection.

With that said, the common eval is a real metaprogramming.

let str = `(function hello(){
    console.log('hello');
})()`;
eval(str);//>> hello

With the above code, eval can write a computer program to dynamically generate a section of the program, and realize the use of the program to create a program, which is meta-programming.

And if the program has the ability to make its own, then this programming language has the ability to reflect. The above JavaScript is to make your own: by entering a JavaScript string, a new JavaScript function is created, so JavaScript has the ability to reflect.

 
Share this