Another Array of Records in Pas2JS

Top 

Example code : Another Array of Records in Pas2JS

type

  // Declare a customer record

  TCustomer = Record

    firstName : string;

    lastName  : string;

    address1  : string;

    address2  : string;

    address3  : string;

    city      : string;

    postCode  : string;

end;

 

type

  TExpandedMember = record

  SubmitType : string;

  Member : TCustomer;

end;

 

 

procedure TForm1.W3Button11Click(Sender: TObject);

var SubTypeMember : TExpandedMember;

    i : integer;

begin

 SubTypeMember.SubmitType := 'VCL';

 SubTypeMember.Member.firstName := 'vcl Programmer';

 SubTypeMember.Member.address3 := 'vcl@aboutguide.com';

 SubTypeMember.Member.postCode := '555';

end;

Note that Record fields can have default values. Types for fields with default values can be type-inferenced. 

 

JS output:

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

 

var SubTypeMember = {SubmitType:"Java",Member:{address1:"",address2:"",address3:"",city:"",

firstName:"",lastName:"",postCode:""}};

      var i$11 = 0;

      SubTypeMember.SubmitType = "VCL";

      SubTypeMember.Member.firstName = "vcl Programmer";

      SubTypeMember.Member.address3 = "vcl@aboutguide.com";

      SubTypeMember.Member.postCode = "555";