JavaScript: Reflect.get

By Xah Lee. Date: . Last updated: .

New in JS2015.

Reflect.get(obj, key)
  • Similar to obj[key], but in a function form, so can be constructed at run-time.
  • Get the value of a property, own property or in Prototype Chain.
  • If key is not found, return undefined.
  • If obj is not Object Type object, throw a TypeError exception.
Reflect.get(obj, key , thisValue)

thisValue is passed as this Binding to a getter property call.

[see Getter/Setter Properties]

const u = {p:3};

console.log( Reflect.get ( u , "p" ) === 3 );

// if property key is not found, return undefined
console.log( Reflect.get ( u , "h" ) === undefined );
// Reflect.get looks up prototype chain

const x = { p: 3 };

const y = Object.create(x);
// x is parent of y

console.log( Reflect.get(y, "p") === 3);

Getter Property and ThisValue Example

// create a object with 2 properties: p1 and p2. The p2 is a getter property.
const xx = {
    p1: 1,
    get p2() {
        console.log("getter called");
        return this.p1;
    }
};

// create some unrelated object with key p1
const yy = {p1:"wow"};

console.log(
 Reflect.get ( xx , "p2" , yy) === "wow"
);
// prints "getter called"

[see Getter/Setter Properties]

See also: Access Property

JavaScript: Get Property

BUY
ΣJS
JavaScript in Depth

JS Obj Reference

Reflect