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. Advertising

  2. Kozman wrote:
    > 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();


    You are misusing arrays. Use and object instead.

    var MyArr = {};

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

  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!



    Douglas Crockford wrote:
    > Kozman wrote:
    > > 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();

    >
    > You are misusing arrays. Use and object instead.
    >
    > var MyArr = {};
    >
    > http://javascript.crockford.com/
     
    Kozman, Sep 9, 2006
    #3
  4. In article <>,
    Kozman <> writes
    >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.

    <snip>

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

    John
    --
    John Harris
     
    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.


    John G Harris wrote:
    > In article <>,
    > Kozman <> writes
    > >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.

    > <snip>
    >
    > Be aware that all objects have some properties names already in use (in
    > modern browsers). E.g. toString.
    >
    > John
    > --
    > John Harris
     
    Kozman, Sep 14, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    7,519
    Mark Bole
    Mar 4, 2005
  2. Audun Røe

    XSLT "associative" arrays

    Audun Røe, Apr 14, 2004, in forum: XML
    Replies:
    10
    Views:
    1,149
    Mike Conmackie
    Apr 26, 2004
  3. desktop
    Replies:
    5
    Views:
    408
    James Kanze
    Jun 26, 2007
  4. shashi
    Replies:
    17
    Views:
    489
    Guest
    Apr 13, 2006
  5. Robert Mark Bram

    length of an associative array

    Robert Mark Bram, Aug 7, 2005, in forum: Javascript
    Replies:
    7
    Views:
    99
    Richard Cornford
    Aug 7, 2005
Loading...

Share This Page