This question is related to What are the best practices for declaring an array in Javascript?
Let's say the client, call them "DB Cooper" , has the first requirement that the following code must be executed before any other JavaScript code:
Array = function(){ alert('Mwahahahaha'); };
In addition, Cooper requires custom functions to be added to the built-in Array
object (and not to the captured one). For example, if Array
was hacked, this will be done with:
Array.prototype.coolCustomFunction = function(){ alert('I have ' + this.length + ' elements! Cool!'); };
That will allow:
var myArray = []; myArray.coolCustomFunction();
However, this is incompatible with the first requirement. Thus, how can you best meet both requirements of DB Cooper?
Note: DB even wrote a test script to help decide if it meets his requirements ... what a guy!
Update: For those of you who love the challenge: try finding an awkward cross-browser solution to this problem. For example, here is an even more restrained test case (thanks for reformatting this Berga) that captures Array, Object, Array.prototype.constructor and Object.prototype.constructor. It still looks like there might be a specific browser solution for this (see Bergi to comment on his answer , and let us know if you find a way to steal it in FF), but itβs not yet clear if there is a cross-browser solution for this.
source share