public
Created

meta proxy as global __proto__

  • Download Gist
proxy-global-builtin.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
Window.prototype.__proto__ = forwarder(Window.prototype.__proto__);
 
function forwarder(target){
var traps = {
getOwnPropertyDescriptor: Object.getOwnPropertyDescriptor.bind(null, target),
getOwnPropertyNames: Object.getOwnPropertyNames.bind(null, target),
keys: Object.keys.bind(null, target),
defineProperty: Object.defineProperty.bind(null, target),
get: function(r,k){ return target[k] },
set: function(r,k,v){ target[k] = v; return true },
has: function(k){ return k in target },
hasOwn: function(k){ return {}.hasOwnProperty.call(target, k) },
delete: function(k){ delete target[k]; return true },
enumerate: function(){ var i=0,k=[]; for (k[i++] in target); return k }
};
 
var names = {
get: 1,
set: 1,
has: 0,
hasOwn: 0,
delete: 0,
defineProperty: 0,
getOwnPropertyDescriptor: 0
}
 
return Proxy.create(Proxy.create({
get: function(r, trap){
return function(a, b, c, d){
if (trap in names)
console.log(trap, arguments[names[trap]])
else
console.log(trap);
if (trap in traps)
return traps[trap].apply(trap, arguments);
}
}
}), Object.getPrototypeOf(target));
}
proxy-global.js
JavaScript
1 2 3 4 5 6 7 8
Window.prototype.__proto__ = meta.proxy(Window.prototype.__proto__, meta.proxy({}, {
get: function(f, t, trap){
return function(fwd, target, key){
typeof key === 'string' ? console.log(trap, key) : console.log(trap);
return fwd();
}
}
}));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.