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/