The static Reflect.get function is utilized to access a property from an object as though it were a function. The initial parameter represents the object, while the subsequent parameter denotes the name of the property.
Syntax:
Example
Reflect.get(target, propertyKey[, receiver])
Parameters:
target: This refers to the object that serves as the focus for retrieving the property.
propertyKey : It is the name of the key to get.
receiver: This refers to the value that is supplied for the object call when a getter is accessed.
Return value:
It returns the value of the property.
Exceptions:
A TypeError, if the target is not an Object.
Browser Support:
| Chrome | 49 |
|---|---|
Edge |
12 |
| Firefox | 42 |
| Opera | 36 |
Example 1
Example
const u = {p:3};
console.log( Reflect.get ( u , "p" ) === 3 );
// if property key is not found, return undefined just like obj.key
console.log( Reflect.get ( u , "h" ) === undefined );
console.log( Reflect.get ( u , "h" ) === 3 );
Output:
Output
true
true
false
Example 2
Example
const x = {p:3};
const y = Object.create (x);
// x is parent of y
console.log (
Reflect.get ( y, "p" ) === 3
// Reflect.get will traverse the prototype chain to find property
);
Output:
Example 3
Example
const object1 = {
x: 1,
y: 2
};
console.log(Reflect.get(object1, 'y'));
// expected output: 1
var array1 = ['zero', 'one','Carry','marry','charry'];
console.log(Reflect.get(array1, 4));
Output:
Output
2
"charry"