Using Records in Pas2JS

Top 

Example code : Using Records in 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);  // #13#10

end;

 

procedure TForm1.W3Button11Click(Sender: TObject);

var John, Sarah : TCustomer1;

              i : integer;

begin

// Set up the John's customer details

  John.firstName := 'John';

  John.lastName  := 'Smith';

  John.address1  := '7 Park Drive';

  John.address2  := 'Branston';

  John.address3  := 'Grimworth';

  John.city      := 'Banmore';

  John.postCode  := 'BNM 1AB';

 

// Set up John's sister similarly - simply copying the whole record

  Sarah := John;

 

// And then changing the first name to suit

  Sarah.firstName := 'Sarah';

 

// Now show the details of both customers

 ShowCustomer([John.firstName, John.lastName,

                             John.address1, John.address2, John.address3,John.city,

                             John.postCode]);

 

 

 ShowCustomer([Sarah.firstName, Sarah.lastName,

                             Sarah.address1, Sarah.address2, Sarah.address3,Sarah.city,

                             Sarah.postCode]);

end;

John

Smith

7 Park Drive

Branston

Grimworth

Banmore

BNM 1AB

 

Sarah

Smith

7 Park Drive

Branston

Grimworth

Banmore

BNM 1AB

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

JS output:

function ShowCustomer(fields) {

   var i = 0;

   var $temp50;

   for(i = 0,$temp50 = fields.length;i<$temp50;i++) {

      WriteLn($DIdxR(fields,i,""));

   }

   WriteLn("\r\n");

};

 

function W3Button11Click(Self, Sender$7) {

      var John = {address1:"",address2:"",address3:"",city:"",firstName:"",lastName:"",postCode:""};

      var Sarah = {address1:"",address2:"",address3:"",city:"",firstName:"",lastName:"",postCode:""};

      var i$11 = 0;

      John.firstName = "John";

      John.lastName = "Smith";

      John.address1 = "7 Park Drive";

      John.address2 = "Branston";

      John.address3 = "Grimworth";

      John.city = "Banmore";

      John.postCode = "BNM 1AB";

      Copy$TCustomer1(John,Sarah);

      Sarah.firstName = "Sarah";

      ShowCustomer(([John.firstName,John.lastName,John.address1,John.address2,John.address3,John.city,John.postCode].slice()));

      ShowCustomer(([Sarah.firstName,Sarah.lastName,Sarah.address1,Sarah.address2,Sarah.address3,Sarah.city,Sarah.postCode].slice()));

   }