By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,569 Members | 1,181 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,569 IT Pros & Developers. It's quick & easy.

Using "length" as a subscript in associative arrays

P: n/a
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?

Sep 8 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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/
Sep 8 '06 #2

P: n/a
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/
Sep 8 '06 #3

P: n/a
In article <11**********************@i3g2000cwc.googlegroups. com>,
Kozman <mk*****@gmail.comwrites
>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
Sep 9 '06 #4

P: n/a
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 <11**********************@i3g2000cwc.googlegroups. com>,
Kozman <mk*****@gmail.comwrites
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
Sep 14 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.