Reflect.set does not work as intended

It seems unlikely that I found some glaring cross-browser error. But according to Reflect.set documents, Reflect.set supposed to use the 4th parameter as thisArg (for example, if the given variable is a setter). The first argument is the object whose value must be set, but whenever I put an object as the 4th argument, the value is set to it 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?

+5
source share
1 answer

The first argument is an object whose value should be set to

Not really. The first argument is the object whose setters are called (including in the prototype chain of the object).

whenever I put any object as the 4th argument, the value will be set to it instead of the target object.

Yes. Because the property is always set on the receiver. It is just that the argument is optional, as it usually matches the target, and therefore the first argument is used by default if it is not specified.

+2
source

Source: https://habr.com/ru/post/1274347/


All Articles