Using "length" as a subscript in associative arrays

K

Kozman

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

Douglas Crockford

Kozman said:
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/
 
K

Kozman

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 said:
Kozman said:
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/
 
J

John G Harris

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
 
K

Kozman

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top