Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Proxy pretending to be arrays
(function(global){
// Place to store fake arrays
var myProxyArrays = new WeakMap();
global.ProxyArray = (function(){
var handler, call, construct;
// See https://github.com/DavidBruant/ProxyArray for actual implementation
return function(){
var p = Proxy.createFunction(handler, call, construct); //
myProxyArrays.set(p, "whatev's"); // Value doesn't matter. Only presence and weak reference to avoid mem leaks
return p;
};
})();
// redefining Object.prototype.toString
var NativeOptS = Object.prototype.toString;
Object.prototype.toString = function(){
return myProxyArrays.has(this) ?
"[Object Array]" : // pretending it's an array
NativeOptS.apply(this, arguments);
};
// redefining Array.isArray
var NativeIsArray = Array.isArray;
Array.isArray = function(o){
return myProxyArrays.has(o) ?
true : // pretending it's an array
NativeIsArray(o);
};
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.