delete methods added to array prototype ?

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
 
M

Martin Honnen

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))
{
...
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top