Records



The Record keyword is one of the most useful, and distinguishing features of Delphi (and the Pascal language). It provides a means of collecting together a set of different data types into one named structure. Each field in the structure is referenced in the manner record.field.

Create simple javascript object with Records
{ "test" : { "somevalue" : 12 }, "text" : "this is a string value" }
Example code parsing a JSON object to a Variant
{ "RowID" : 10, "FirstName" : "abc", "LastName" : "def", "YearOfBirth" : 20, "YearOfDeath" : 30, "Sexe" : 1, "Simple" : { "F" : "", "G" : [], "H" : { "H1" : 0, "H2" : "", "H3" : { "H3a" : false, "H3b" : null } }, "I" : "", "J" : [{ "J1" : 1, "J2" : "", "J3" : "reLast" } ] } }
Example code creating a JSON object with Records
{ "F" : "warleyalex", "G$3" : ["Delphi", "Java", "C++"], "H$1" : { "H1" : 123, "H2" : "Jesus Cristo", "H3" : { "H3a" : true, "H3b" : "Smart Mobile Studio" } }, "I" : 41864.09710032407, "J" : [] }

Array of Records in Pas2JS

In practice, records are often more complex. Additionally, we may also have a lot of them, and might store them in an array. Since a record type is legitimate as any other Pas2JS type, we can have a field of a record be a record itself. Here are some examples using Records in Pas2JS.

Using Records in Pas2JS


Using Array of Records with Pas2JS


Array of Records with Const in Pas2JS


Create single javascript object with Records {x: 10, y: 33}


Another Records with Procedure in Pas2JS


Another Array of Records in Pas2JS


Record fields with obfuscation

Once that code is obfuscated, record fields when serialized, becomes giberrish which is a bit of a problem if you store it in local storage and then update the app (field names will change). There's a little black magic:


type
TMyRecord = Record
  mrMagic: Integer; //Magic word to check
  mrData: Array of String; // your string data
End; 
{"oBM":3405691582,"zA4":["first","second"]}

When you activate though, the record fields will get renamed randomly. There's a little black magic to prevent the field to be obsfucated, use the property keyword.

type
TMyRecord = Record
  property mrMagic: Integer; //Magic word to check
  property mrData: Array of String; // your string data

End; 
{"mrMagic":3405691582,"mrData":["first","second"]}

Anonymous Records

Anonymous records allow you to declare arbitrary inline JavaScript objects. More details, see this example: