PDA

View Full Version : [OPEN] Array class getting a remove function from the adapters



Rewand
21 Jun 2007, 1:02 PM
Using version 1.0.1a, tested with prototype and yui adapters.

I'm finding that new Array's are not empty. They contain a function called remove. This seems to be introduced by the adapter classes. Note this example.


<html>
<head>
<script type='text/javascript' src='/ext/adapter/prototype/ext-prototype-adapter.js'>
</script>
</head>

<body onload="testArray();">

<div id="mydiv"></div>

</body>
</html>
<script>
function testArray(){
var a = new Array();
a.push("dummy element");
var s = "";
for(var i in a){
s += (typeof(a[i]) + "<br>");
}
document.getElementById("mydiv").innerHTML = s;
}
</script>
With the adapter included the results are


string
function

With the adapter not included the results are

string

Is this a known issue? Is there a fix?

jack.slocum
21 Jun 2007, 1:28 PM
It's not an element in the array, it's a function on the Array prototype. for(var key in obj) is not iterating the array, it's iterating the object.

exo
22 Jun 2007, 12:46 AM
Please don't remove remove(). I love that little function. =D>

As an alternative to for(x in y) you can use for(var x; x < y.length; x++), then you won't get the remove() function.

jack.slocum
22 Jun 2007, 5:12 AM
Don't worry we can't remove it, we use it as well. :)

cchiriac
22 Jun 2007, 7:04 AM
Please don't remove remove(). I love that little function. =D>

As an alternative to for(x in y) you can use for(var x; x < y.length; x++), then you won't get the remove() function.

I'm using this syntax too, but for a lookup table with the key on the record's id it's not reliable. Fortunaletly Jack has posted a simple workaround so the that "foreach" can be used too.

http://extjs.com/forum/showthread.php?t=3603&highlight=for+array+object
Check out post 5. In my case I had to add a comparison to 'undefined' as my keys map database ids. It seems to work fine for me.



Ext.each(grids, function(grid, index, grids){
if (typeof grid != 'function' && typeof grid != 'undefined'){
grid.getSelectionModel().clearSelections();
}
}, this);

tryanDLS
22 Jun 2007, 9:39 AM
There are numerous discussions on the web regarding the differences between iterating over an array with for(x in y) vs for(var x; x < y.length; x++), and why you shouldn't use the former unless you understand what's going on.

Rewand
22 Jun 2007, 10:11 AM
Thanks

This works for me