Javascript – Is it possible to deregister Javascript prototype extension

javascriptprototype

I understand that using for..in loop to iterate over Array is not the recommended way for iteration. But I am curious to know if there is a way to deregister these extensions. I have worked with Groovy and in Groovy's case there is a registry which can be manipulated during runtime to erase the additional entries specified through metaclass.

My current use-case is that Prototype is adding extensions to Array prototype. Since I use numerous plugins based on jQuery (I do use jQuery), I have to test most functionality of the website to ensure that the everything is fine. That's quite daunting. As to why I should use Prototype and jQuery, well lets just say that, I am left with no options at the moment.

I also understand that deregistering (if possible) these extensions is in no way the solution for all the code to exist in harmony, since it means a disruptive change to the existing instances and new instances as well. There would be numerous anonymous functions which would depend on these extensions and would fail later. I am just plain to curious to know about the javascript implementation and possible access to the underlying logic.

Thanks
Ram

Best Solution

The problem with the for loop is that some libs (prototype) add properties to the base Object and that causes them to be enumerated by the for loop.

The following loop will loop through all the custom properties added to the base Object and delete them from the prototype, making it safe to use "for in" with arrays

for (var prop in {}) {
  delete Object.prototype[prop]   
}

However, as you have mentioned, this will break prototype itself. If you use prototype, you should use one of their iteration methods. But I didn't understand what you were trying to get at with that question so I just explained how to undo what prototype did.