Using "length" as a subscript in associative arrays

Giganews Newsgroups
Subject: Using "length" as a subscript in associative arrays
Posted by:  Kozman (mkozi…
Date: 8 Sep 2006

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

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 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?