1. #1
    Sencha Premium Member
    Join Date
    May 2007
    Posts
    3
    Vote Rating
    0
    mqaa is on a distinguished road

      0  

    Default 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

  2. #2
    Ext User
    Join Date
    Mar 2007
    Posts
    39
    Vote Rating
    0
    stekolla is on a distinguished road

      0  

    Default


    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.

  3. #3
    Sencha User
    Join Date
    Mar 2007
    Posts
    56
    Vote Rating
    0
    sdrew is on a distinguished road

      0  

    Default


    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.

  4. #4
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    17
    jack.slocum will become famous soon enough jack.slocum will become famous soon enough

      0  

    Default


    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.
    Jack Slocum
    Ext JS Founder
    Original author of Ext JS 1, 2 & 3.
    Twitter: @jackslocum
    jack@extjs.com

  5. #5
    Sencha User
    Join Date
    Mar 2007
    Posts
    7,854
    Vote Rating
    4
    tryanDLS is on a distinguished road

      0  

    Default


    You'll find numerous discussions of this problem via Google.

  6. #6
    Ext JS Premium Member NoahK17's Avatar
    Join Date
    Apr 2008
    Location
    Atlanta, GA
    Posts
    518
    Vote Rating
    1
    NoahK17 is on a distinguished road

      0  

    Default


    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

  7. #7
    Sencha - Community Support Team mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
    mystix will become famous soon enough

      0  

    Default


    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];
      }
    }

  8. #8
    Sencha User
    Join Date
    Apr 2011
    Posts
    3
    Vote Rating
    0
    kenorb is on a distinguished road

      0  

    Default


    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

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..." hd porno faketaxi