JS: Reflect.set
New in JS2015.
Reflect.set(obj, key, val)
-
Set a value to a proprety of object. Return
true
if success, elsefalse
.This function is similar to the syntax
obj[key] = val
, but in a function form. Function form lets you easily manipulate it at run-time.const xx = {}; Reflect.set(xx, "p", 3); console.log(xx.hasOwnProperty("p")); console.log(xx["p"] === 3);
// example of failed attempt to set property const xx = {}; Object.preventExtensions(xx); console.log(Reflect.set(xx, "p", 3) === false); console.log(xx.hasOwnProperty("p") === false);
Reflect.set(obj, key, val, thisValue)
-
thisValue is passed as value of this Binding for
Getter Setter Properties.
let aa = []; const jj1 = { p1: 1, set p2(x) { aa = [this.p1, x]; }, }; const jj2 = { p1: 2 }; Reflect.set(jj1, "p2", 1); console.log(aa); // [ 1, 1 ] Reflect.set(jj1, "p2", 1, jj2); console.log(aa); // [ 2, 1 ]