PDA

View Full Version : I wonder whether Ext changes javascript arrays?



vocabo
30 Oct 2009, 4:03 AM
If I execute this JavaScript code in a browser (ie6, ff3)

var a = [1, 2, 3];
for ( var i in a )
alert(i);

the result is as expected to be:
0, 1, 2
But when I add the Ext 3.0 files

<script type="text/javascript" src="js/ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="js/ext/ext-all.js"></script>
<link rel="stylesheet" type="text/css" href="js/ext/resources/css/ext-all.css" />
and start the same code the result is:

0, 1, 2, remove in ff3 and

0, 1, 2, indexOf in ie6
If I start
alert('remove')

function (b) {
var a = this.indexOf(b);
if (a != -1) {
this.splice(a, 1);
}
return this;
}
How does this 'remove' appear?

Jamie Avins
30 Oct 2009, 4:58 AM
You should never use "for (idx in arr)" to iterate an array. Iterating an array is for example: "for (idx = 0; idx < arr.length; idx++)".

Mike Robinson
30 Oct 2009, 6:38 AM
As Jaimie points out, the implementation of "for..in" is not always consistent, and not always correct. The behavior of an "old-fashioned" iterated loop is predictable... and, afaik, equivalent.

Animal
30 Oct 2009, 6:43 AM
for...in is consistent.

It iterates through properties of an object.

So this will include all an object's own properties (Those that are assigned in a literal eg obj = {propname: 'value'} or those that are poked in eg: obj.propname = 'value')

And this will also include prototype properties which will include methods of an object (or any other properties that were inserted into the prototype)

This is why when you iterate through an object with for...in, you must test for hasOwnProperty if you want to skip methods.

VinylFox
30 Oct 2009, 7:07 AM
As Jaimie points out, the implementation of "for..in" is not always consistent, and not always correct...

Thats not what Jamie said, nor is it a correct statement.

Jaitsu
30 Oct 2009, 9:50 AM
for...in is consistent.

It iterates through properties of an object.

So this will include all an object's own properties (Those that are assigned in a literal eg obj = {propname: 'value'} or those that are poked in eg: obj.propname = 'value')

And this will also include prototype properties which will include methods of an object (or any other properties that were inserted into the prototype)

This is why when you iterate through an object with for...in, you must test for hasOwnProperty if you want to skip methods.

touche

Mike Robinson
30 Oct 2009, 10:53 AM
I graciously concede... ~o)

vocabo
31 Oct 2009, 2:46 PM
You should never use "for (idx in arr)" to iterate an array. Iterating an array is for example: "for (idx = 0; idx < arr.length; idx++)".
How to iterate an array with non numeric values?

I will follow the Animal's advise and will try using hasOwnProperty().

Animal
31 Oct 2009, 2:49 PM
errrrm, you not read this thread?

Do not use for...in

Use numeric indices. It's an Array.



var a = [1, 2, 3];
for (var i = 0; i < a.length; i++) {
console.log(a[i]);
}


"alert"? what are you doing?? alert???? sheesh!

vocabo
31 Oct 2009, 2:52 PM
errrrm, you not read this thread?

Do not use for...in

Use numeric indices. It's an Array.



var a = [1, 2, 3];
for (var i = 0; i < a.length; i++) {
console.log(a[i]);
}
"alert"? what are you doing?? alert???? sheesh!

You are too fast. I just edited my post. :)

Animal
31 Oct 2009, 3:27 PM
Do not use hasOwnProperty for Arrays. Just use a numeric index.

vocabo
1 Nov 2009, 1:44 AM
Do not use hasOwnProperty for Arrays. Just use a numeric index.
I do! But in this case I need associative array. As far as I know there isn't associative array implementation in the Ext framework. So I use array with non numeric indices instead of associative array and iterate with for in.
I figured out where these 'remove' and 'indexOf' came from. The Ext array class add them.

Animal
1 Nov 2009, 2:28 AM
Ah.

A PHP programmer?

"Associative array" is just an Object in Javascript.

An object has named properties you can iterate over with for...in

To avoid prototype properties, you test hasOwnProperty

vocabo
1 Nov 2009, 3:12 AM
May be this is the best solution, provided that I use Ext. I found many implementations of associative arrays:

http://mootools.net/docs/core/Native/Hash
http://www.mojavelinux.com/articles/javascript_hashes.html

but I don't know how they will interact with Ext and for now, I don't have enough time to test them.

Animal
1 Nov 2009, 7:05 AM
Use an object:



foo = {};
foo['bar'] = 'bletch';