Skip to content

Instantly share code, notes, and snippets.

@DavidBruant
Created June 15, 2011 12:11
Show Gist options
  • Save DavidBruant/1026960 to your computer and use it in GitHub Desktop.
Save DavidBruant/1026960 to your computer and use it in GitHub Desktop.
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