-
10 May 2007 1:14 AM #1
Problem with Array.indexOf / Array.remove
Problem with Array.indexOf / Array.remove
Hi there,
I just started to us Ext and like it alot!
But there is an issue with the extensions it add to the array object using:
Ext.applyIf(Array.prototype, {
indexOf : function(o){
for (var i = 0, len = this.length; i < len; i++){
if(this[i] == o) return i;
}
return -1;
},
remove : function(o){
var index = this.indexOf(o);
if(index != -1){
this.splice(index, 1);
}
}
});
We are using the following style to go through the items in an array:
arrayX = [];
arrayX['key1'] = 'foo';
arrayX['key2'] = 'bar';
for(var key in arrayX) {
var element = arrayX[key];
}
But with the addition of indexOf and remove this no longer works because these functions are now also retruned as key's in the array.
Is there a way to avoid this problem?
Marc
-
10 May 2007 5:52 AM #2
The easy solution to this is not to use an array unless you're using numeric indices. Replace the array literal with an object literal and what you're trying to do should work fine.
-
10 May 2007 12:59 PM #3
I just ran into this BUG also, adding Ext to an existing application broke existing javascript functions as Ext mucked with the javascript Array prototype.
This is a big problem that I'm sure many will run into and can't expect everyone to change their existing javascript functions. There must be a better solution?
Steve.
-
10 May 2007 1:12 PM #4
The bug isn't in Ext, it's in the misuse of the Array class. If you look at the functions on the Array object they all work with indexes. Arrays themselves do not support string keys. When you add string key'ed properties, do you notice the length doesn't change? You can't slice or pop or splice them?
However, every object in JavaScript supports dynamic expando properties. When you make a call like this:
arrayX['key1'] = 'foo';
you are not doing anything with the Array class, which is why the Array functions don't work on that value. You are simply assigning an expando to that JavaScript object. In fact, arrayX.key1 = 'foo'; does exactly the same thing.
If all you want is a JavaScript object to add expandos to, the syntax is almost identical:
arrayX = {};
arrayX['key1'] = 'foo';
arrayX['key2'] = 'bar';
Lastly, why is this in BUGS?? If you want to discuss something that Ext is doing that you don't agree with, put it in the general discussion forum. That doesn't mean it's a bug.
-
10 May 2007 1:14 PM #5
You'll find numerous discussions of this problem via Google.
Tim Ryan
Read BEFORE posting a question / BEFORE posting a Bug
Use Google to Search - API / Forum
API Doc (4.x | 3.x | 2.x | 1.x) / FAQ / 1.x->2.x Migration Guide / 2.x->3.x Migration Guide
-
10 Jun 2008 1:57 PM #6
I've had the same issue... and I was told quite forcefully by the developers that the method as coded is not going to change, unless you over-ride it yourself.
Which is what I did.Noah
Senior Web Developer
NBA.com
-
12 Jun 2008 10:16 PM #7
if you're going to iterate through a js Array using for..in as opposed to the traditional subscript operator, i'd strongly advise using the built-in Object.prototype.hasOwnProperty method to guard against js libraries which tack on stuff to the Array prototype.
e.g.
Code:for(var key in arrayX) { if (arrayX.hasOwnProperty(key)) { var element = arrayX[key]; } }
Sencha Docs / Ext 3.x - ( Docs | Examples )
Learning Center / Saki's Examples (for 2.x) / HOWTO - ( Report Bugs | Post Proper Code )
-
27 May 2011 4:11 AM #8
The same problem described here:
http://www.sencha.com/forum/showthre...-page&p=591739
The problem exists as well with Flag module in Drupal.
See: http://drupal.org/node/1170762


Reply With Quote