Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Using "length" as a subscript in associative arrays

Reply
Thread Tools

Using "length" as a subscript in associative arrays

 
 
Kozman
Guest
Posts: n/a
 
      09-08-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
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?

 
Reply With Quote
 
 
 
 
Douglas Crockford
Guest
Posts: n/a
 
      09-08-2006
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/
 
Reply With Quote
 
 
 
 
Kozman
Guest
Posts: n/a
 
      09-08-2006
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/


 
Reply With Quote
 
John G Harris
Guest
Posts: n/a
 
      09-09-2006
In article <(E-Mail Removed). com>,
Kozman <(E-Mail Removed)> 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
 
Reply With Quote
 
Kozman
Guest
Posts: n/a
 
      09-14-2006
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 <(E-Mail Removed). com>,
> Kozman <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why "associative" in associative container? desktop C++ 5 06-26-2007 07:49 AM
Difference between accessing arrays and associative arrays using a int index shashi Perl Misc 17 04-13-2006 03:48 PM
Associative arrays in Oracle PL/SQL as parameters, use with Java googledude@hotmail.com Java 4 03-04-2005 01:12 AM
XSLT associative arrays Eric Anderson XML 9 10-11-2004 11:25 PM
XSLT "associative" arrays Audun Rĝe XML 10 04-26-2004 12:26 AM



Advertisments