Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.

    •  
      CommentAuthorjskopek
    • CommentTimeMar 4th 2006 edited
     permalink
    I've been working with classes in a manner that I believe only works with the prototype.js (or prototype.lite.js) scripts, so a solution for this problem may be difficult or impossible to apply elsewhere. With that warning, here it is:

    I'm looking for a way to list all class-based elements in the same way that one could list elements of a certian class (document.getElementsByClassName("aClass") ). Take, for example, this script:

    <script type="text/javascript" src="prototype.js"></script>
    <script type="text/javascript">
    fighter = Class.create();

    fighter.prototype = {
    initialize: function(health, name) {
    this.name = name;
    this.health = health;
    },
    output: function() {
    alert(this.name);
    }
    };

    window.onload = function() {
    a = new fighter(100,"Bob");
    b = new fighter(200,"John");
    c = new fighter(300, "Billy");
    }
    var a, b, c;
    </script>


    Note: this script was written entirely in-post, so I cannot test it and appologize if there are any scripting errors. The <html><body> etc. tags are not present for simplicity.
    What I'm looking for is a way to list a, b, and c without having to place them into an array when I initialize them - in other words, by calling functions like this:
    alert( document.getElementsByType("fighter").length );
    I can't seem to find anything online that illustrates this capabilitiy in javascript. Is this even possible?
    Thanks ahead of time :)
    •  
      CommentAuthorjskopek
    • CommentTimeMar 5th 2006
     permalink
    Still havent figured out a way to do it, but if anyone's interested the 'lame' way works great.

    Define an array at the root level so it's accessible anywhere in the code. Say
    var fighters = new Array();

    and then just go fighters(fighters.length) = new fighter(blabla);
Add your comments
    Username Password
  • Format comments as (Help)