Can anyone explain how does it (reflection and build-in Reflect object) work in JS in human language and with simple examples.
Wikipedia: "In computer science, reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at runtime".
1. To iterate over the members of an object
var person = {
fname: "Default",
lname: "Default",
getFullName: function(){
return this.fname + " " + this.lname;
}
}
var john = {
fname: "John",
lname: "Doe"
}
john.__proto__ = person;
//Reflection : Iterate over the members of an object
for(var prop in john){
console.log(prop + " : " + john[prop]);
}
OUTPUT :

NOTE : When examining the properties of an object, JavaScript’s default is to include inherited properties.
obj.hasOwnProperty(prop)
var person = {
fname: "Default",
lname: "Default",
getFullName: function(){
return this.fname + " " + this.lname;
}
}
var john = {
fname: "John",
lname: "Doe"
}
john.__proto__ = person;
//Reflection : Iterate over the direct or own members of an object
for(var prop in john){
if(john.hasOwnProperty(prop)){
console.log(prop + " : " + john[prop]);
}
}
OUTPUT :

var person = {
fname: "Default",
lname: "Default",
getFullName: function(){
return this.fname + " " + this.lname;
}
}
var john = {
fname: "John",
lname: "Doe"
}
john.__proto__ = person;
//Reflection : Iterate over the direct or own members of an object using Object.getOwnPropertyNames()
console.log(Object.getOwnPropertyNames(john));//["fname", "lname"]
//Reflection : Getting the property descriptor for an own property of object
console.log(Object.getOwnPropertyDescriptor(person, 'getFullName'));//{writable: true, enumerable: true, configurable: true, value: ƒ}
Here:
The static method Object.defineProperty() defines a new property directly on an object, or modifies an existing property on an object, and returns the object.
Object.defineProperty(obj, prop, descriptor)
2. To examine all the properties of an object - If we want to distinguish between properties and methods of an object, we can use the operator typeofthat returns a string that identifies the type of the analyzed element.
//Reflection : To examine all the properties of an object
for(var prop in person){
if(typeof person[prop] != 'function'){
console.log(prop + " : " + john[prop]);
}
}
//OUTPUT:
//fname : John
//lname : Doe
3. To examine all the methods of an object
//Reflection : To examine all methods of an object
for(var prop in person){
if(typeof person[prop] == 'function'){
console.log(prop + " : " + john[prop]);
}
}
//OUTPUT:
//getFullName : function(){
// return this.fname + " " + this.lname;
// }
JavaScript already has reflection features in ES5 even though they were not named reflection either by specification or by the community. Methods such as Array.isArray , Object.getOwnPropertyDescriptor and Objects.keys acted much like the features reflection exhibits. The Reflect built-in in ES6 now houses methods in this category.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With