Can someone explain the following output
var handler = {
get: function(target,key, receiver){
return new Proxy(Reflect.get(target, key, receiver),handler);
},
apply: function(target, thisArg, args){
Reflect.apply(target, thisArg, args);
}
}
var p = new Proxy(window, handler);
p.alert("Alert"); // Throws illegal invocation error
var alias = p.alert;
alias("Alert") // Even this works which is baffling for me
var p = new Proxy(Reflect.get(window, "alert"), handler);
p("Alert"); // works as expected
The first statement throws an illegal invocation error, whereas the second one doesn't. Both of them look identical to me, and I don't understand how the first statement doesn't have the required this context during Reflect.apply
alert.call(new Proxy(window, {}), "alert")window. Explicit context: in bothobj.funcandfunc.call(obj)the context will beobj.