Javascript Array.push fix breaks for(var x in myarray)

A

Andrew Phillipo

In IE5 I am working with a lot of code that uses

Array.push()

as well as

for(var i in myArray) {}

Great - so I'm fixing the Array.prototype.push function for browsers
without it. That much works great. However as soon as I start
traversing arrays with for(... in ...) I end up with additional
elements at the end of the array.

A colleague said it was down to the added methods being seen as part of
the array , which to be sounds slightly strange!

Any ideas are welcome!

Thanks,

Andy.
 
L

Lasse Reichstein Nielsen

Andrew Phillipo said:
Array.push() ....
for(var i in myArray) {}

Great - so I'm fixing the Array.prototype.push function for browsers
without it. That much works great. However as soon as I start
traversing arrays with for(... in ...) I end up with additional
elements at the end of the array.

Yes, the element "push".
A colleague said it was down to the added methods being seen as part of
the array , which to be sounds slightly strange!

Not really. The "for(..in..)" construct iterates over all properties
of an object (not marked as DONT_ENUM). It's not restricted to arrays,
and does nothing special for arrays, so it will find the "push" property
you have added.
Any ideas are welcome!

Use "for(var i = 0; i < arr.length; i++) {" instead of "for(var i in array){".

/L
 
M

Michael Winter

In IE5 I am working with a lot of code that uses
[snip]

for(var i in myArray) {}

Well then, don't. Iterate over the array using the length property:

for(var i = 0, n = myArray.length; i < n; ++i) {
/* ... */
}

[snip]
[...] I end up with additional elements at the end of the array.

As you should. A for..in statement will walk over all enumerable
properties, which will include both user-defined properties (the array
elements) and methods.

[snip]

Mike
 
M

Martin Honnen

Andrew said:
In IE5 I am working with a lot of code that uses

Array.push()

as well as

for(var i in myArray) {}

Great - so I'm fixing the Array.prototype.push function for browsers
without it. That much works great. However as soon as I start
traversing arrays with for(... in ...) I end up with additional
elements at the end of the array.

No, for .. in does not traverse an array, it enumerates the properties
of any object, and enumeration might show methods as well as they are
simply function properties.

If you want to loop through an array in JavaScript use
for (var i = 0; i < myArray.length; i++) {
// access myArray
}

Enumeration is useful for objects but even there you can of course run
into that trouble that someone for instance has added properties or
methods to Object.prototype. That is why you can find debates whether or
not someone writing a JavaScript library should add methods to
Object.prototype or not.

If you need enumeration you can distinguish methods with typeof checks e.g.

Object.prototype.someMethod = function () {}

var god = { name: 'Kibo', home: 'http://www.kibo.com/' };

var result = '';

for (var propertyName in god) {
result += propertyName + ': ' + (typeof god[propertyName]) + '\r\n';
}

alert(result);


In most implementations by now you can also use hasOwnProperty to find
out whether it is a property of the object itself or one in the
prototype chain:

Object.prototype.someMethod = function () {}

var god = { name: 'Kibo', home: 'http://www.kibo.com/' };

var result = '';

for (var propertyName in god) {
result += propertyName + ': ' + (typeof god[propertyName]) +
(god.hasOwnProperty ? '; hasOwnProperty("' + propertyName + '"): ' +
god.hasOwnProperty(propertyName) : '') + '\r\n';
}

alert(result);



But IE 5/Win with the JScript 5 it installs does not support
hasOwnProperty if recollection serves me right.
 

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

Members online

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top