Defining static arrays in Pas2JS

Top 

Example code : How to define arrays in Pas2JS

type

   TDays = array[1..7of string;

   TCars  = (Ford, Vauxhall, GM, Nissan, Toyota, Honda);

   TEnum  = (Mon=5, Tue, Wed, Thu, Fri, Sat, Sun);

// Static arrays are supported as value types, they have a fixed size with user-specified bounds: 

   TZeroToTen  =  array  [0..10]  of  Integer;

   TTenToTwenty  =  array  [10..20]  of  String;

// Multi-dimensional arrays are supported, with two forms:

   TCompactForm  =  array  [1..3,  1..5]  of  Float;

   TVerboseForm  =  array  [1..3]  of  array  [1..5]  of  Float;

 

   TIntArray = array [3..5of Integer;

   TIntArr  = array of Integer;

   TStringArr = array of String;

   TFloatArr = array of Float;

 

   const Days : TDays = ('Mon','Tue','Wed','Thu','Fri','Sat','Sun');

// Constant static arrays can be initialized using square brackets [  ]: 

   const  a: array[0..2of String = ['zero','one','two'];

// Constant static/dynamic arrays can be initialized using standard Delphi syntax: 

   const  b:  array[0..2of String = ('zero','one','two');

   var  staticArray:  array  [0..2]  of  String;

   var  dynamicArray:  array of String;

 

 

procedure TForm1.W3Button2Click(Sender: TObject);

 var

 cars    : array[TCars] of string;          // Range is 0..5

 japCars : array[Nissan..Honda] of string;  // Range is 3..5

 myEnum  : TEnum;                           // Range is 5..11

 myArray : TIntArray;                       // Range is 3..5

 myChar  : TIntArr;                         // Range is 0..-1

 myShort : TStringArr;                      // Range is 0..-1

 myNum   : TFloatArr;                       // Range is 0..-1

 myZeroToTen      : TZeroToTen;             // Range is 0..10

 myTenToTwenty    : TTenToTwenty;           // Range is 10..20

 myCompactForm    : TCompactForm;           // Range is 1..3

 myVerboseForm    : TVerboseForm;           // Range is 1..3

 

begin

WriteLn('Low(cars)  = '+IntToStr(Low(cars)));

WriteLn('High(cars)  = '+IntToStr(High(cars)));

 

WriteLn('Low(japCars)  = '+IntToStr(Low(japCars)));

WriteLn('High(japCars)  = '+IntToStr(High(japCars)));

 

WriteLn('Low(myEnum)  = '+IntToStr(Low(myEnum)));

WriteLn('High(myEnum)  = '+IntToStr(High(myEnum)));

 

WriteLn('Low(myArray)  = '+IntToStr(Low(myArray)));

WriteLn('High(myArray)  = '+IntToStr(High(myArray)));

 

WriteLn('Low(myChar)  = '+IntToStr(Ord(Low(myChar))));

WriteLn('High(myChar)  = '+IntToStr(Ord(High(myChar))));

 

WriteLn('Low(myShort)  = '+IntToStr(Low(myShort)));

WriteLn('High(myShort)  = '+IntToStr(High(myShort)));

 

WriteLn('Low(myNum)  = '+IntToStr(Low(myNum)));

WriteLn('High(myNum)  = '+IntToStr(High(myNum)));

 

WriteLn('Low(myZeroToTen)  = '+IntToStr(Low(myZeroToTen)));

WriteLn('High(myZeroToTen)  = '+IntToStr(High(myZeroToTen)));

 

WriteLn('Low(myTenToTwenty)  = '+IntToStr(Low(myTenToTwenty)));

WriteLn('High(myTenToTwenty)  = '+IntToStr(High(myTenToTwenty)));

 

WriteLn('Low(myCompactForm)  = '+IntToStr(Low(myCompactForm)));

WriteLn('High(myCompactForm)  = '+IntToStr(High(myCompactForm)));

 

WriteLn('Low(myVerboseForm)  = '+IntToStr(Low(myVerboseForm)));

WriteLn('High(myVerboseForm)  = '+IntToStr(High(myVerboseForm)));

 

end;

 

Result is:

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

Low(cars)  = 0

High(cars)  = 5

 

Low(japCars)  = 3

High(japCars)  = 5

 

Low(myEnum)  = 5

High(myEnum)  = 11

 

Low(myArray)  = 3

High(myArray)  = 5

 

Low(myChar)  = 0

High(myChar)  = -1

 

Low(myShort)  = 0

High(myShort)  = -1

 

Low(myNum)  = 0

High(myNum)  = -1

 

Low(myZeroToTen)  = 0

High(myZeroToTen)  = 10

 

Low(myTenToTwenty)  = 10

High(myTenToTwenty)  = 20

 

Low(myCompactForm)  = 1

High(myCompactForm)  = 3

 

Low(myVerboseForm)  = 1

High(myVerboseForm)  = 3

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

 

JS output:

function W3Button2Click(Self, Sender$5) {

      var cars = ["","","","","",""];

      var japCars = ["","",""];

      var myEnum = 0;

      var myArray = [0,0,0];

      var myChar = [];

      var myShort = [];

      var myNum = [];

      var myZeroToTen = [0,0,0,0,0,0,0,0,0,0,0];

      var myTenToTwenty = ["","","","","","","","","","",""];

      var myCompactForm = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]];

      var myVerboseForm = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]];

      WriteLn("Low(cars)  = 0");

      WriteLn("High(cars)  = 5");

      WriteLn("Low(japCars)  = 3");

      WriteLn("High(japCars)  = 5");

      WriteLn("Low(myEnum)  = 5");

      WriteLn("High(myEnum)  = 11");

      WriteLn("Low(myArray)  = 3");

      WriteLn("High(myArray)  = 5");

      WriteLn("Low(myChar)  = 0");

      WriteLn(("High(myChar)  = "+((myChar.length-1)).toString()));

      WriteLn("Low(myShort)  = 0");

      WriteLn(("High(myShort)  = "+((myShort.length-1)).toString()));

      WriteLn("Low(myNum)  = 0");

      WriteLn(("High(myNum)  = "+((myNum.length-1)).toString()));

      WriteLn("Low(myZeroToTen)  = 0");

      WriteLn("High(myZeroToTen)  = 10");

      WriteLn("Low(myTenToTwenty)  = 10");

      WriteLn("High(myTenToTwenty)  = 20");

      WriteLn("Low(myCompactForm)  = 1");

      WriteLn("High(myCompactForm)  = 3");

      WriteLn("Low(myVerboseForm)  = 1");

      WriteLn("High(myVerboseForm)  = 3");

   }

 

var TEnum = { 5:"Mon", 6:"Tue", 7:"Wed", 8:"Thu", 9:"Fri", 10:"Sat", 11:"Sun" };

var TCars = [ "Ford", "Vauxhall", "GM", "Nissan", "Toyota", "Honda" ];

var Days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];