Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflect.set not working as intended

It seems unlikely that I found some kind of glaring cross-browser bug. But according to the documentations Reflect.set is supposed to use the 4th parameter as the thisArg (for example if the set variable is a setter). The first argument is the object the value is to be set on, yet whenever I supply any object as the 4th argument, the value gets set on that instead of the target object.

var target = new Object;
var thisArg = new Object;

Reflect.set(target, 'variable', 52, thisArg);

target.variable == undefined
thisArg.variable == 52

Any explanation?

like image 482
John Smith Avatar asked Oct 29 '22 21:10

John Smith


1 Answers

The first argument is the object the value is to be set on

Not exactly. The first argument is the object whose setters are invoked (including those on the object's prototype chain).

whenever I supply any object as the 4th argument, the value gets set on that instead of the target object.

Yes. Because the property always gets set on the receiver. It's just that the argument is optional because it's usually the same as the target, and therefore defaults to the first argument when not supplied.

like image 189
Bergi Avatar answered Nov 15 '22 05:11

Bergi