User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 397,808 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,510 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 4933 | Replies: 4
Reply
Join Date: Nov 2006
Posts: 2
Reputation: hwfa is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
hwfa hwfa is offline Offline
Newbie Poster

Help Javascript Structures?

  #1  
Nov 11th, 2006
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Location: India
Posts: 1,289
Reputation: vishesh is on a distinguished road 
Rep Power: 4
Solved Threads: 32
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: Javascript Structures?

  #2  
Nov 11th, 2006
You have defined person as an object not as an array. You can probably ise multi-dimensional arrays for that.
Reply With Quote  
Join Date: Nov 2006
Posts: 2
Reputation: hwfa is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
hwfa hwfa is offline Offline
Newbie Poster

Re: Javascript Structures?

  #3  
Nov 13th, 2006
Originally Posted by vishesh View Post
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.
Reply With Quote  
Join Date: Oct 2006
Location: India
Posts: 1,289
Reputation: vishesh is on a distinguished road 
Rep Power: 4
Solved Threads: 32
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: Javascript Structures?

  #4  
Nov 14th, 2006
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
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 47
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

Re: Javascript Structures?

  #5  
Nov 14th, 2006
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:
I think that the main difference between objects and arrays is that objects can contain inner functions.

...apparently not true:
var test = new Array();
test.test = function(){document.write("werd")};
test["hello"] = 45;
document.write(test["hello"]);
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 7:56 am. Reason: self != correct
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 6:17 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC