Created
June 15, 2011 12:11
-
-
Save DavidBruant/1026960 to your computer and use it in GitHub Desktop.
Proxy pretending to be arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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