•
•
•
•
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
![]() |
•
•
Join Date: Nov 2006
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
•
•
Join Date: Nov 2006
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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.
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
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
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation:
Rep Power: 5
Solved Threads: 47
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:
...apparently not true:
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.
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();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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
- Opinions? javascript/php/etc and programming standards (JavaScript / DHTML / AJAX)
- JavaScript's window.opener (JavaScript / DHTML / AJAX)
- Javascript/HTML problem!!! (JavaScript / DHTML / AJAX)
- Javascript Useful? (IT Careers and Business)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Question about FORM submit
- Next Thread: Validation script cant seem to work



Linear Mode