| | |
Javascript Structures?
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Nov 2006
Posts: 2
Reputation:
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:
Solved Threads: 0
•
•
•
•
You have defined person as an object not as an array. You can probably ise multi-dimensional arrays for that.
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
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.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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 8:56 am. Reason: self != correct
Plato forgot the nullahedron..
![]() |
Similar Threads
- 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 Professionals' Lounge)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Question about FORM submit
- Next Thread: Validation script cant seem to work
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp animate array automatically beta box bug calendar cart checkbox class codes column cookies createrange() css cursor date debugger decimal design dom download dropdown editor element embed enter error explorer firefox focus frameworks getselection google gwt hint html htmlform ie7 iframe images index internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jsp jump listbox maps masterpage math menu microsoft mimic mp4 object onmouseover parent paypal php player position post problem programming progressbar prototype redirect regex runtime safari scale scriptlets search select session shopping size sql text textarea toggle variables w3c website window windowofwords windowsxp wysiwyg \n






