JAVASCRIPT

Reflection


Reflection in programming refers to the ability of a program to examine and modify its own structure and behavior during runtime. 
Reflection allows us to inspect and manipulate objects, properties, and functions dynamically. This capability enables tasks such as accessing object properties by name, iterating over object properties, dynamically creating, and invoking functions, and modifying object prototypes. Reflection is a powerful feature that provides flexibility and extensibility to JavaScript programs, allowing them to adapt to changing requirements and conditions at runtime.
 

Example:

// Create an object
const person = {
    name: 'John',
    age: 30,
    greet: function() {
    console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`);
    }
};

// Access object properties dynamically using reflection
console.log(person['name']); // Output: John
console.log(person['age']); // Output: 30

// Invoke object methods dynamically using reflection
person['greet'](); // Output: Hello, my name is John and I'm 30 years old.

// Iterate over object properties using reflection
for (let key in person) {
    console.log(`${key}: ${person[key]}`);
}
Output:
name: John
age: 30
greet: [Function: greet]