JavaScript: Reflect.set

By Xah Lee. Date: . Last updated: .

New in JS2015.

Similar to obj[key] = val.

thisValue is passed as value of this Binding for Getter/Setter Properties.

Returns true if success, else false.

const ee = {};
Reflect.set ( ee , "p", 3 )
console.log( ee ); // { p: 3 }

Example of failure:

// example of failed attempt to set property
const x = {};
Object.preventExtensions ( x );

const returnValue = Reflect.set ( x , "p", 3 );

console.log( x ); // {}
console.log( returnValue ); // false

Using thisValue

Here's a example using the 4th argument.

let zz = [];

const obj1 = {
    xx : 1,
    set yy (x) { zz = [this.xx , x] ; },
};

const obj2 = { xx: 2};

Reflect.set( obj1, "yy", 1);
console.log( zz ); // [ 1, 1 ]

Reflect.set( obj1, "yy", 1, obj2);
console.log( zz ); // [ 2, 1 ]

Difference Between Reflect.Set and Property Assignment Syntax

Reflect.set is similar to the operator form obj[key] = val.

The difference are:

See also: Access Property

JS in Depth
XAH  BUY NOW

JS in Depth

JS Obj Ref

DOM


JS Obj Ref

Reflect