Arrays of Records example in Pas2JS

Top 

Example code : Arrays of Records in Pas2JS ex 1

type

TPerson = record

  FirstName, LastName: string ;

  BirthDate: Float ;

  BirthPlace: string ;

end;

 

procedure TForm1.W3Button8Click(Sender: TObject);

var

  MyPeople: Array[0..2of TPerson ;

  i:Integer ;

begin

  for i := 0 to 2 do

  begin

    MyPeople[i].FirstName := 'MyPeople[' + IntToStr(i) + '].FirstName' ;

    MyPeople[i].LastName := 'MyPeople[' + IntToStr(i) + '].LastName';

    MyPeople[i].BirthDate := Now + i;

    MyPeople[i].BirthPlace := 'MyPeople[' + IntToStr(i) + '].BirthPlace';

  end ;

 

  for i := 0 to 2 do

  begin

     WriteLn('Nome: '+MyPeople[i].FirstName + ' ' + MyPeople[i].LastName);

     WriteLn('DataNasc: '+DateToStr(MyPeople[i].BirthDate));

     WriteLn('Cidade: '+MyPeople[i].BirthPlace);

     WriteLn(#13);

  end;

end;

Nome: MyPeople[0].FirstName MyPeople[0].LastName

DataNasc: 2014-08-07

Cidade: MyPeople[0].BirthPlace

 

 

Nome: MyPeople[1].FirstName MyPeople[1].LastName

DataNasc: 2014-08-08

Cidade: MyPeople[1].BirthPlace

 

 

Nome: MyPeople[2].FirstName MyPeople[2].LastName

DataNasc: 2014-08-09

Cidade: MyPeople[2].BirthPlace

----------------------------------------------------

 

JS output:

function W3Button8Click(Self, Sender$11) {

      var MyPeople = [{FirstName:"",LastName:"",BirthDate:0,BirthPlace:""},{FirstName:"",LastName:"",BirthDate:0,BirthPlace:""},{FirstName:"",LastName:"",BirthDate:0,BirthPlace:""}];

      var i$10 = 0;

      for(i$10 = 0;i$10<=2;i$10++) {

         MyPeople[$Idx(i$10,0,2,"")].FirstName = "MyPeople["+i$10.toString()+"].FirstName";

         MyPeople[$Idx(i$10,0,2,"")].LastName = "MyPeople["+i$10.toString()+"].LastName";

         MyPeople[$Idx(i$10,0,2,"")].BirthDate = Now()+i$10;

         MyPeople[$Idx(i$10,0,2,"")].BirthPlace = "MyPeople["+i$10.toString()+"].BirthPlace";

      }

      for(i$10 = 0;i$10<=2;i$10++) {

         WriteLn(("Nome: "+MyPeople[$Idx(i$10,0,2,"")].FirstName+" "+MyPeople[$Idx(i$10,0,2,"")].LastName));

         WriteLn(("DataNasc: "+DateToStr(MyPeople[$Idx(i$10,0,2,"")].BirthDate)));

         WriteLn(("Cidade: "+MyPeople[$Idx(i$10,0,2,"")].BirthPlace));

         WriteLn("\r");

      }

   }