// Reflect API provides ability to manage object properties/methods
// at run-time. All the methods of Reflect are static.
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
};
const args = ['John', 'Doe'];
// construct method is equivalent to new operator
const john = Reflect.construct(Person, args); // john = new Person(args[0],args[1])
console.log(john.lastName); // Doe
// has(object, propertyKey): returns true if object has specified property
console.log(Reflect.has(john, "firstName"));
// apply(function to call, value of "this", arguments)
// Below executes charAt on firstName.
// Output we get is 'n': 4th char in first name
console.log(Reflect.apply("".charAt, john.firstName, [3]));
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
};
let args = ['John', 'Doe'];
let john = Reflect.construct(
Person,
args
);
console.log(john instanceof Person);
console.log(john.fullName); // John Doe
Code language: JavaScript (javascript)