delete methods added to array prototype ?

  • Thread starter Thread starter gerry
  • Start date Start date
G

gerry

I have to do some work with a 3rd party javascipt library that adds methods
to the Array prototype.
Of course this messes up anything that does a for...in expecting to only get
items contained within the array as it now returns the additional method
names as well.

I came up with this little routine that works perfectly in IE but does
nothing in any other browser :
function cleanArray( arr )
{
if ( !( arr && arr instanceof Array ) )
arr = new Array();
for( var key in arr )
{
if ( typeof(arr[key]) == 'function' )
delete arr[key];
}
return arr;
}

So it looks like MS is implementing delete in a useful but not standard way.
Is there any other way to get/create a 'clean' array without the extra
methods that have been added to the Array prototype ?

Gerry
 
gerry said:
I have to do some work with a 3rd party javascipt library that adds methods
to the Array prototype.
Of course this messes up anything that does a for...in expecting to only get
items contained within the array as it now returns the additional method
names as well.

Use a classic for loop to access the array items:
for (var i = 0, l = a.length; i < l; i++)
{
var item = a;
}

As for the for..in loop, you can at least check
for (var p in a)
{
if (a.hasOwnProperty(p))
{
...
}
}
 
Back
Top