Skip to content Skip to sidebar Skip to footer

Overriding Array Literal In Javascript

My app (a custom web browser) is injecting code into a page that has a modified Array prototype (specifically because it uses prototype.js). I would like to ensure that the code I

Solution 1:

No, there isn't. This is actually one of the good things about using literals. :-) But in your particular unusual situation, it's less helpful.

Just for completeness:

Given that you do apparently have a reference to an Array.prototype that isn't modified (I'm basing this on your ArrayWithUnmodifiedPrototype symbol), you could get around this by swapping things each time you need to create an array or call one of those third-party libraries. (This is obviously less than ideal.)

E.g.:

var oldPrototype = Array.prototype;
Array.prototype = ArrayWithUnmodifiedPrototype.prototype;
/*...call third-party lib that might use []...*/Array.prototype = oldPrototype;

But if the third-party stuff is processing things in ajax callbacks and such, you'd probably struggle to slip your code in to swap the prototypes around.

Probably the best thing is to ensure the code you inject works with any reasonable modifications to Array.prototype (such as the ones PrototypeJS does, which are mostly reasonable despite the fact it still overwrites things like Array.prototype.map even if they're there...).

Post a Comment for "Overriding Array Literal In Javascript"