What is reflect in JavaScript?
I found myself wondering if there is a way to do so-called reflection in JavaScript.
I mean you can use eval() or JSON.parse(), but is there some API that would let you actually reflect something on, eg, objects of type String? Or can JavaScript only do so-called meta-programming, like writing your own syntax interpreter? If not, which languages are capable of it and what is their tradeoff?
I am asking because I am currently writing a compiler for a language (that looks nothing like JavaScript) and to build my IR, I have made a simple, light-weight reflection API that allows me to inspect properties, read fields, etc. It works pretty well. But I just need to know if this feature is available for me to use. I am curious about the tradeoff of it not existing in JavaScript, but rather, having the flexibility I have today.
Why is reflection such an important piece of the software engineering pie? Let me offer a few examples. It's a really nice safety net when writing tools for your colleagues or teams. For example, if a tool has an option to add all the members of your social circle and you realize that if the names were wrong then nobody would be able to find them in your address book, you could have automatically fixed this issue during the build step.
I have also seen lots of projects that have the goal to create test-suites with hundreds of test cases, with each test case being built on top of another test case. With a reflective API we could go even deeper and write our own reflection API to create our own reflection API. Which, I don't see any real reason why we cannot have that in JavaScript today.
To make it clear, let's see what does a "simple" (in theory) reflection API look like, we could easily extend it in the sense that you get access to methods as well.
When to use reflection JavaScript?
Why we should use reflection
In JavaScript, the most used and powerful feature is Function. It allows to create dynamic classes and functions that can be called as methods. The code is short and easy to understand. When we need a function that calls another one. We can use a function name and a function as parameter. When we need a class that calls another one. We can use a constructor name and the constructor as parameter. In other languages, it's quite different. You need to pass all parameters when calling the function. And if you need to call several functions from another one. You need to pass each parameter when calling. In this case, there are usually complex classes that take parameters. We need to create a new instance of it. And call the constructor with the parameters. We don't need to use reflection to create the same class. In JavaScript, we can create objects directly. That means that we can create objects without parameters. Without a constructor, without a new keyword and with a prototype. For example, we can create an object without parameters and a constructor. Const obj = Object.create(null); In the example, we have an empty constructor. We don't need to pass anything to create an object. Then, we can add a method to the object.create(null); obj.method = function() We have an object, but we don't have any method.create(null); obj.method = function() When we need to add a method to an object. We need to use reflection. In the example, we have an object. And we don't need a constructor or a new keyword. We need to call the constructor and use the method we want to add to the object.create(null); obj.method = function() In the examples, we have an object that doesn't need any constructor. And we add a method to it.
Related Answers
What is reflect metadata used for?
I am wondering when I should use js reflect in development. Now I only do t...
What is JavaScript extension?
I am working on an experimental project that uses JavaScript...
What is the difference between reflect and proxy?
If you are using a language with dynamic typing, the diff...