Additional Programmer-Defined Array Properties

K

Kenneth Jacker

I know that "length" is a standard property of arrays ...

But, can the programmer create their own *new* array properties?

Something like this:

var a = new Array();

a[0].foo = 3;
a[1].foo = 7;
... ... ...

I can't seem to get it to work ...

Thanks for any help,
 
C

Captain Paralytic

I know that "length" is a standard property of arrays ...

But, can the programmer create their own *new* array properties?

Something like this:

  var a = new Array();

  a[0].foo = 3;
  a[1].foo = 7;
  ... ... ...

I can't seem to get it to work ...
Something like:

var a = [{"foo":3},{"foo":7}];
document.write(a[0].foo);
document.write("<br />");
document.write(a[1].foo);
 
M

Martin Honnen

Kenneth said:
I know that "length" is a standard property of arrays ...

But, can the programmer create their own *new* array properties?

Arrays are not different from other objects and you can always add
properties e.g.
var a = new Array(); // or var a = [];
a.foo = 42;
Something like this:

var a = new Array();

a[0].foo = 3;
a[1].foo = 7;

But that looks more like an attempt to add properties to elements in the
array and not to the array itself. If you put objects into your array
then you can of course add properties e.g.
var a = new Array();
a[0] = new Object();
a[0].foo = 3;
or shorter as
var a = [ {foo: 3 } ];
 
T

Thomas 'PointedEars' Lahn

Kenneth said:
I know that "length" is a standard property of arrays ...

But, can the programmer create their own *new* array properties?

Yes, as long as they do not collide with the built-in ones.
Something like this:

var a = new Array();

a[0].foo = 3;
a[1].foo = 7;
... ... ...

But here you would be creating the `foo' property on elements of the array
encapsulated by the Array object referred to by `a'. That is not what is
usually understood as "array property", but e.g.

a.foo = 3;

would (cf. a.length).
I can't seem to get it to work ...

The above cannot work because you can only create properties on objects.
a[0] and a[1] do not refer to objects, the property access must result in
`undefined' (the array has been initialized with no elements, its length
property value is 0.) If you had initialized the array with

var a = new Array({}, {});

or

var a = [{}, {}];

(or other values that can be converted to a object that can be augmented
with properties each), it would have worked because each {} creates a new
Object instance (with only inherited properties) and results in a reference
to that object.

Your error description could and should have been a bit more verbose, see
also said:
Thanks for any help,

Seriously, I read "Prof Kenneth J Hacker" the first time¹ :)


HTH

PointedEars
___________
¹ probably inspired by ESR's "J Random Hacker"
 
S

SteveYoungTbird

Jonathan said:
Kenneth said:
I know that "length" is a standard property of arrays ...

But, can the programmer create their own *new* array properties?

Something like this:

var a = new Array();

a[0].foo = 3;
a[1].foo = 7;
... ... ...

I can't seem to get it to work ...

a[0] === undefined;
a.foo = 'hi';
a.foo === 'hi' || ddt();

a[1] = {};
a[1].foo = 3;
a[1].foo === 3 || ddt();

What is ddt()?

Regards, Steve.
 
L

Lasse Reichstein Nielsen

Kenneth Jacker said:
I know that "length" is a standard property of arrays ...

But, can the programmer create their own *new* array properties?

Depends on what you mean.
You can add properties to array objects just as on any other objects.
Arrays are objects, they just treat the length property specially
(and, implicitly, properties with names that are array indices).
Something like this:

var a = new Array();

a[0].foo = 3;
a[1].foo = 7;
... ... ...

I can't seem to get it to work ...

This won't work. At this point, the object referenced by "a" has no
property called "0", so reading it returns undefined. Trying to set
a "foo" property on the undefined value fails.

If you want to add properties to each array element, the elements
must be objects. E.g.,

var a = [];
a[0] = {};
a[1] = {};

a[0].foo = 3;
a[1].foo = 7;

(The entire initialization can be done using literals:
a = [{foo: 3}, {foo: 7}];
)

/L
 
K

Kenneth Jacker

Kenneth Jacker writes:
I know that "length" is a standard property of arrays ...

Yikes, going back through this group shows that I *never thanked*
everyone for their help!
Guess I "solved my problem", then selfishly went on to the "next
thing".

Sorry about that ...


I do appreciate the time folks took to reply to my question.

-Kenneth
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top