Using "length" as a subscript in associative arrays

Discussion in 'Javascript' started by Kozman, Sep 8, 2006.

  1. Kozman

    Kozman Guest

    I have a problem where I need to use the literal "length" as a
    subscript in an associative array (I have no control over what is used
    as a subscript..."length" happens to be one of the uncontrolled
    values). The problem is that if I assign it to something other than an
    integer, it complains and throws an exception:

    MyArr["length"] = new SomeObject();

    I understand the importance of the length property in ordered
    lists...but it has no usage in the associative array world.

    I've tried using "prototype" to overload the length property as
    follows:

    function CustomArray()
    {
    this.length = function() { alert("In");};
    }
    CustomArray.prototype = new Array();

    This suppresses the exception but i cannot see my alert() pop up when I
    try to call length "as a function". Its like the javascript engine is
    blocking any attempt to overload this property.

    The only recourse I have at this point is to mangle the subscripts
    before they are assigned and then demangle them when they are
    accessed...However, this doesn't solve the problem...it only masks
    it...all it takes is a future project where someone forgot to mangle
    their subscripts for this problem to rear it ugly head again.

    Has anyone else run into this problem?...if so, what technique did you
    use to solve it?
     
    Kozman, Sep 8, 2006
    #1
    1. Advertisements

  2. You are misusing arrays. Use and object instead.

    var MyArr = {};

    http://javascript.crockford.com/
     
    Douglas Crockford, Sep 8, 2006
    #2
    1. Advertisements

  3. Kozman

    Kozman Guest

    Thanks for the reply Douglas.

    I actually ran into this problem when I leveraged microsoft's
    webservices.htc file (which is written in JS). When a WSDL is pulled
    in that defines "length" as an XSD element, the script throws an
    exception.

    To state your answer a different way...don't use [] or Array() to
    initialize your variable if you are going to use it as an associative
    array.

    The problem:

    var MyArr = new Array();
    MyArr["aaaaa"] = new Object();
    MyArr["length"] = new Object(); // Throws an exception


    The solution:

    var MyArr = new Object(); /// ...OR... var MyArr = {};
    MyArr["aaaaa"] = new Object();
    MyArr["length"] = new Object(); // Ok


    Thanks!



     
    Kozman, Sep 9, 2006
    #3
  4. <snip>

    Be aware that all objects have some properties names already in use (in
    modern browsers). E.g. toString.

    John
     
    John G Harris, Sep 9, 2006
    #4
  5. Kozman

    Kozman Guest

    Understood...I performed a test to see if the following will not cause
    an execption:

    var MyArr = new Object();
    MyArr["toString"] = new Object();
    MyArr["toString"].memVar = "vfdsvfds";

    Thankfully, it doesn't...however, the downside is that toString() as a
    function will be replaced with toString "the object". For my
    situation, this is acceptable risk as the associative array is being
    used strictly for map storage/retrieval purposes only.

    Thanks for the response John.
     
    Kozman, Sep 14, 2006
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.