SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ie and object.prototype

    Hello

    i am trying to use the following code.
    it work perfectly with firefox, konqueror, opera, but not with ie (foo.length = 0 despite i add some object).

    Does anybody know a solution for this issue ?

    Many thanks

    --- cut
    listeCommandes.prototype = new Array;
    function listeCommandes(){
    this.ajouter = function (obj){
    this.push(obj);
    }
    this.supprimer = function (index){
    this.splice(index,1);
    }

    this.totalht = 0;
    this.totalttc = 0;
    this.totaltva = 0;
    }

    var foo = new listeCommandes()

    foo.push("ee");
    foo.ajouter("ee");

    alert(foo.length);

    --- cut

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function listeCommandes() {
        this.ajouter = function(obj) { this.push(obj); }
        this.supprimer = function(index) { this.splice(index, 1); }
        this.totalht = 0;
        this.totalttc = 0;
        this.totaltva = 0;
    }
    
    listeCommandes.prototype = Array.prototype;
    
    
    var foo = new listeCommandes();
    
    foo.push("ee");
    foo.ajouter("ee");
    
    alert(foo.length);
    This alerts "2", although I only tested it in Opera 9.20.
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    on ie, this alert 0.

    i explain the problem by to way:

    - on the first hand, this.length does seems to work very well.
    i 'm trying to make a function like this

    this.longueur = function (){
    if (this.length){
    return this.length
    }
    else {
    for (x=0;this[x];x++){
    }
    return x;
    }
    but it does'nt work as i want

    - on the second hand, i think push() and push() alias (ajouter()) also doesn't work very good

    i use the following code for test:
    foo.push("eeee");
    alert(foo[0]);
    alert(foo.length);
    alert(foo.longueur());

    foo.push("eee");
    alert(foo[1]);
    alert(foo.length);
    alert(foo.longueur());


    foo.push("ee");
    alert(foo[2]);
    alert(foo.length);
    alert(foo.longueur());
    on firefor it simply work (also work if i add comment some line in longueur() function for forcing use of the counter with the for(){} )

    on ie
    - alert(foo[1]); alert undefined
    - and alert(foo[2]) also

  4. #4
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This simplier and quicker code that i use for testing may be usefull:

    tab.prototype = new Array();
    function tab(){
    }

    var letab = new tab()

    alert(letab.push("eeeeeeee")); // push return length witch is equal to 1
    alert(letab[0]);
    alert(letab.length); // now length is equal to 0 !!!
    Does anybody know another way to extend a Array ?

    loving internet explorer ....

  5. #5
    doRighteousDeeds++
    Join Date
    Aug 2006
    Location
    تركيا Turkey Türkiye
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
      
    <script type="text/javascript">
    var foo = [];
    // or var foo = new Array();
    alert(foo.length); // 0
    
    foo.push("eeee");
    alert(foo[0]);  // eeee
    alert(foo.length); // 1
    alert(foo);  // eeee
    
    foo.push("eee");
    alert(foo[1]);  // eee
    alert(foo.length);  // 2
    alert(foo);  // eeee,eee
    
    
    foo.push("ee");
    alert(foo[2]); // ee
    alert(foo.length);  // 3
    alert(foo); // eeee,eee,ee
    
    </script>
    Last edited by muazzez; Apr 27, 2007 at 13:22.

  6. #6
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems to work.

    and it work also if i add this code for having functions i my object:
    foo.bar = function (){
    alert('bar');
    }
    I don't know why but foo = Array; and foo = []; seems to be definitly different.

    that 's not the way i wish to find but many thanks, i don't find a best solution
    ( was reading http://tobielangel.com/2007/2/8/javascript-subclassing and http://dean.edwards.name/weblog/2006/11/hooray/ )

    Many many thanks another time ! (i losed lot of time while searching a solution)

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Third Stone From The Sun
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Array is a constructor for an array. [ ] is an instance of an Array.

    gebura - You'll probably have to create your own collections when you need them. You might keep an internal array as a property.

    Code:
    ListeCommandes = function ListeCommandes( type ) {
        this.type = type.
        this._items = [ ];
    };
    
    ListeCommandes.prototype = {
    
        ,addCommands :  function( commandArray ) {
    // might need a typeCheck here.
            this._items = this._items.concat( commandArray );
        }
    
        ,getItems : function() { 
            return this._items.concat(); // Defensive copy; see Bloch.
        }
     ... 
    };
    You could use YUI extend and create an abstract Collection superclass, then have a List class that extends collection, then have ListeCommands extend List.

    Code:
    YAHOO.extend( com.acme.ListeCommands, com.acme.List, {
    // ListeCommands' List override methods here.
    });
    I've used Stack functionality for CommandStack, so I can have push, pop. This makes undo/redo very easy. (this may be not what ur trying to do). Iterator functionality is nice too. Collections are very useful. You just have to write and them yourself in javascript. It is not easy.
    My outdated site is down for a while now.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •