943,963 Members | Top Members by Rank

Ad:
Nov 11th, 2006
0

Javascript Structures?

Expand Post »
I'd like to build a structure in Javascript. I think it will be an array in which each element will have multiple properties.

For example
var person = new Object();
person[0].firstName="John";
person[0].lastName="Smith";
person[0].phoneNumber="555 1234";

person[1].firstName="Jane";
person[1].lastName="Robinson";
person[1].phoneNumber="555 6789";

etc.

and then things like
document.write(person[0].firstName);

This doesn't seem to work.

What am I doing wrong? Is there a way to achieve this type of structure in Javascript?

I've looked in various tutorials but none of the examples cover this type of thing.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hwfa is offline Offline
2 posts
since Nov 2006
Nov 11th, 2006
0

Re: Javascript Structures?

You have defined person as an object not as an array. You can probably ise multi-dimensional arrays for that.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Nov 13th, 2006
0

Re: Javascript Structures?

Click to Expand / Collapse  Quote originally posted by vishesh ...
You have defined person as an object not as an array. You can probably ise multi-dimensional arrays for that.
In fact, this code works:
function Person(firstName, lastName, phoneNumber) {
this.firstName = firstName;
this.lastName=lastName;
this.phoneNumber=phoneNumber;
}

person = new Object();

person[0] = new Person("John","Smith","555 1234");
person[1] = new Person("Bill","Robinson","555 6789");
person[2] = new Person("Jane","Doe","555 6753");

document.writeln("<ol>")
for (personNumber in person) {
document.writeln("<li>" + person[personNumber].lastName + ", " + person[personNumber].firstName + ": " + person[personNumber].phoneNumber + "</li>");
}
document.writeln("</ol>");

It works regardless of whether I use Array or Object. Objects and arrays seem to be synonymous - if someone would give me a clear explanation of the difference I'd much appreciate it. The trick is to make each array element an object in its own right and to have a constructor for the class.

Took a while; ho hum.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hwfa is offline Offline
2 posts
since Nov 2006
Nov 14th, 2006
0

Re: Javascript Structures?

Nice work, it didn't got into my mind.
Well Arrays are objects only and collection of well defines similer objects(somewhat like SETS in maths ). Its's also stored in a single variable name and you need to know index number of a an elemnet to access that paricular element. On the other hand objects are your own special kind of data, a collection of methods and properties whatever. With objects you could create your own data types. And you of course need to know the variable names to access the data not index number.

Hope this has made the difference clear.

vishesh
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Nov 14th, 2006
0

Re: Javascript Structures?

the way you're using them, they are synonymous.

arrays store indexed values, but they can also store values by associative keys. Objects can store values by associative keys also; so, when you write person[0] = la, you're creating a key (0) which you can then use to retrieve the Person-class objects from the generic Object named person. You can store values in objects using the more object-orientated "dot" syntax, without ever creating Object classes. Methinks thats bad practice though.

I think that the main difference between objects and arrays is that objects can contain inner functions.

Generally, you should use arrays to store indexed or keyed Objects or values, and you should probably avoid using the generic "Object" entirely. It reduces the strength of the code you're writing: you never really know what "Object" has inside it, or what it does.

EDIT:
Quote ...
I think that the main difference between objects and arrays is that objects can contain inner functions.
...apparently not true:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var test = new Array();
  2. test.test = function(){document.write("werd")};
  3. test["hello"] = 45;
  4. document.write(test["hello"]);
  5. test.test();
does exactly what it says on the tin.

well. you certainly can't prototype, class or extend the Array object.

Come to think of it, you probably can. JavaScript is a bit of a weird one.
Last edited by MattEvans; Nov 14th, 2006 at 8:56 am. Reason: self != correct
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Question about FORM submit
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Validation script cant seem to work





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC