Prototypify: Running Prototype code with legacy code
Aaron Newton, Product Manager at CNET sent us an email about his issues of using Prototype in production at a large company like CNET:
I've made a lot of use of Prototype but using it has been a bit of a controversy here at our network. Specifically, the extensions that are inherent in Prototype often create problems for (poorly written) legacy code and advertising javascript that we must include in our environment to make a buck.
Because it's not reasonable for our technical producers to regress every ad that runs on our site, I hired a good friend (and far more talented programmer) to help me find a solution. He, one Alex Cruikshank, authored a chunk of code (2.25K) for us that I think could be of great use to those out there who avoid Prototype because of the way it alters Arrays and Objects; specifically, those trying to use your library in an environment where they do not control 100% of the javascript included.
The code, all 3K of it, is a wrapper of sorts that removes all the extensions to Array and Object from Prototype. Then, when you want to make use of Prototype, you just apply this wrapper to your code.
We have put the code up for Prototypify along with the documentation.
-
-
Prototypify = function() {
-
-
}
-
-
Prototypify.prototypified = false;
-
-
// store then remove functions from prototypes of native objects
-
Prototypify.arrayFunctionHolder = new Object()
-
for (x in Array.prototype)
-
{
-
Prototypify.arrayFunctionHolder[x] = Array.prototype[x];
-
delete Array.prototype[x];
-
}
-
-
Prototypify.stringFunctionHolder = new Object()
-
for (x in String.prototype)
-
{
-
Prototypify.stringFunctionHolder[x] = String.prototype[x];
-
delete String.prototype[x];
-
}
-
-
Prototypify.numberFunctionHolder = new Object()
-
for (x in Number.prototype)
-
{
-
Prototypify.numberFunctionHolder[x] = Number.prototype[x];
-
delete Number.prototype[x];
-
}
-
-
-
Prototypify.proxy = function( f, proxyArguments )
-
{
-
return function()
-
{
-
var needsPrototypes = ! Prototypify.prototypified;
-
if ( needsPrototypes )
-
{
-
Prototypify.prototypified = true;
-
for (x in Prototypify.arrayFunctionHolder)
-
Array.prototype[x] = Prototypify.arrayFunctionHolder[x];
-
for (x in Prototypify.stringFunctionHolder)
-
String.prototype[x] = Prototypify.stringFunctionHolder[x];
-
for (x in Prototypify.numberFunctionHolder)
-
Number.prototype[x] = Prototypify.numberFunctionHolder[x];
-
}
-
-
if ( proxyArguments )
-
{
-
for ( var i=0; i <arguments.length; i++ )
-
if ( typeof arguments[i] == 'function' )
-
arguments[i] = Prototypify.proxy( arguments[i], proxyArguments );
-
}
-
-
var out = f.apply( this, arguments );
-
-
if ( needsPrototypes )
-
{
-
for ( x in Array.prototype )
-
delete Array.prototype[x];
-
for ( x in String.prototype )
-
delete String.prototype[x];
-
for ( x in Number.prototype )
-
delete Number.prototype[x];
-
Prototypify.prototypified = false;
-
}
-
return out
-
}
-
}
-
-
Prototypify.instrument = function( clazz, proxyArguments )
-
{
-
for ( prop in clazz.prototype )
-
{
-
if ( typeof clazz.prototype[prop] == 'function' )
-
clazz.prototype[ prop ] = Prototypify.proxy( clazz.prototype[ prop ], proxyArguments );
-
}
-
}
-
-
Prototypify.instrumentStatic = function( clazz, proxyArguments )
-
{
-
for ( prop in clazz )
-
{
-
if ( typeof clazz[prop] == 'function' )
-
clazz[ prop ] = Prototypify.proxy( clazz[ prop ], proxyArguments );
-
}
-
}
-
Copyright
Copyright (c) 2006 CNET Networks, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

