i'm trying create proxy of dom element, in example used the window object. when run code, an:
uncaught typeerror: illegal invocation
however if modify: return reflect.apply(method, this, arguments);
with: return reflect.apply(method, target, arguments);
works. if create proxy out of self defined object alice both versions works fine.
is there way use return reflect.apply(method, this, arguments);
when creating proxy of dom object? why not working?
<html> <head> </head> <body> hello <script> var alice = { firstname:'alice', lastname:'smith', secret:'secret', amount: 0, removeamount : function(amount){ this.amount = this.amount - amount; }, }; var handler = { get: function(target, name, receiver){ console.log("get: "+ name); var method = reflect.get(target, name, receiver); if (typeof method === "function"){ return function () { return reflect.apply(method, this, arguments); } } return method; }, set: function(target, name, value, receiver){ console.log("set: "+ name); return reflect.set(target, name, value, receiver); } }; var p = new proxy(window,handler); p.alert("http://www.w3schools.com"); </script> </body> </html>
in case want called window.alert
(window context) through proxy
instance (another context), should set window
context explicitly second parameter reflect.apply(method, window, arguments)
. because @ point of code execution, this
doesn't have reference window
anymore.
Comments
Post a Comment