Using Array of Records with Pas2JS

Top 

Example code : Using Array of Records with Pas2JS

// A procedure that displays a variable number of strings

procedure ShowCustomer(const fields: array of string);

var

  i : Integer;

begin

  // Display all fields passed - note : arrays start at 0

  for i := 0 to Length(fields)-1 do

    WriteLn(fields[i]);

    WriteLn(sLineBreak);

end;

 

type

  // Declare a customer record

  TCustomer = Record

    firstName : string;

    lastName  : string;

    address1  : string;

    address2  : string;

    address3  : string;

    city      : string;

    postCode  : string;

end;

 

procedure TForm1.W3Button11Click(Sender: TObject);

var customers : array[1..3of TCustomer;

            i : integer;

begin

// Set up the first customer record

  customers[1].firstName := 'John';

  customers[1].lastName  := 'Smith';

  customers[1].address1  := '7 Park Drive';

  customers[1].address2  := 'Branston';

  customers[1].address3  := 'Grimworth';

  customers[1].city      := 'Banmore';

  customers[1].postCode  := 'BNM 1AB';

 

// Set up the second and third by copying from the first

   customers[2] := customers[1];

   customers[3] := customers[1];

 

   // And then changing the first name to suit in each case

   customers[2].firstName := 'Sarah';

   customers[3].firstName := 'Henry';

 

// Now show the details of both customers

  for i := 1 to Length(customers) do

    ShowCustomer([customers[i].firstName, customers[i].lastName,

                  customers[i].address1, customers[i].address2,

                  customers[i].address3,customers[i].city, customers[i].postCode]);

end;

John

Smith

7 Park Drive

Branston

Grimworth

Banmore

BNM 1AB

 

Sarah

Smith

7 Park Drive

Branston

Grimworth

Banmore

BNM 1AB

 

Henry

Smith

7 Park Drive

Branston

Grimworth

Banmore

BNM 1AB

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

 

JS output:

function W3Button11Click(Self, Sender$7) {

      var customers = [{address1:"",address2:"",address3:"",city:"",firstName:"",lastName:"",postCode:""},{address1:"",address2:"",address3:"",city:"",firstName:"",lastName:"",postCode:""},{address1:"",address2:"",address3:"",city:"",firstName:"",lastName:"",postCode:""}];

      var i$11 = 0;

      customers[(1)-1].firstName = "John";

      customers[(1)-1].lastName = "Smith";

      customers[(1)-1].address1 = "7 Park Drive";

      customers[(1)-1].address2 = "Branston";

      customers[(1)-1].address3 = "Grimworth";

      customers[(1)-1].city = "Banmore";

      customers[(1)-1].postCode = "BNM 1AB";

      Copy$TCustomer(customers[(1)-1],customers[(2)-1]);

      Copy$TCustomer(customers[(1)-1],customers[(3)-1]);

      customers[(2)-1].firstName = "Sarah";

      customers[(3)-1].firstName = "Henry";

      for(i$11 = 1;i$11<=3;i$11++) {

         ShowCustomer(([customers[$Idx(i$11,1,3,"")].firstName,customers[$Idx(i$11,1,3,"")].lastName,customers[$Idx(i$11,1,3,"")].address1,customers[$Idx(i$11,1,3,"")].address2,customers[$Idx(i$11,1,3,"")].address3,customers[$Idx(i$11,1,3,"")].city,customers[$Idx(i$11,1,3,"")].postCode].slice()));

      }

   }

Methods and properties of records are supported too, including "class" methods (same syntax as for classes).