Groups | Blog | Home
all groups > flash actionscript > may 2004 >

flash actionscript : Can someone help me with this syntax?


mellowhappy
5/30/2004 9:47:09 PM
var recData = [{id:0, firstName:"Mick", lastName:"Jones"},
{id:1, firstName:"Joe", lastName:"Strummer"},
{id:2, firstName:"Paul", lastName:"Simonon"}];
userData.items = recData;

I am trying to learn how to use the DataSet components with the Macromedia
Flash Documentation resources. In the example above I do not understand the
use of the syntax with the colons and the curly braces surrounded by the
brackets. I understand generally that the colons are used for data typing- but
here it appears that they are declaring the variables. I thought I understood
the basic syntax until I was reading this example. Can anyone point me to a
reference that discusses this use of the syntax or please explain the pieces to
me.

Thank you so much.
mandingo
5/30/2004 11:52:20 PM
Hi,

The syntax as in use by your example is simply shortened and compact code. If
I was to write the same code in a different way so you could better understand
what is happening, it would appear something like this:

recData = new Array();
myPerson = new Object();
myPerson.id = 0;
myPerson.firstName = "Mick";
myPerson.lastName = "Jones";
recData.push(myPerson);
myPerson = new Object();
myPerson.id = 1;
myPerson.firstName = "Joe";
myPerson.lastName = "Strummer";
recData.push(myPerson);
myPerson = new Object();
myPerson.id = 2;
myPerson.firstName = "Paul";
myPerson.lastName = "Simonon";
recData.push(myPerson);

userData.items = recData;

Now, with both sets of code, you can reference the individual items by array
element and object attribute, like so:

trace(recData[0].firstName + " " + recData[0].lastName + " has data id number:
" + recData[0].id);

Obviously by comparing the code, the three (or four) lines of code you showed
initially is far more compact. The difference being that in the example shown,
when they declare the array, they also define all the objects and attributes at
the same time... this is done for each object by use of the ' { } ' which
defines a new Object, then using the " attribute : value " object-paired
relationship to assign each attribute in the object.

In my more verbose code, at each cycleof the object, I pushed it into the
recData array.

I hope that is enough explanation,
cheers,
mellowhappy
5/31/2004 12:01:49 AM
Thank you very much, you are a great help. Now I understand.

mandingo
5/31/2004 12:05:10 AM
Glad to have assisted,

AddThis Social Bookmark Button