Set the capacity of a array to 3 elements in Pas2JS

Top 

Example code : Set the capacity of a array to 3 elements in Pas2JS

procedure TForm1.W3Button2Click(Sender: TObject);

var wishes : array of string;     // No size given

begin 

  wishes.SetLength(3);    // Set the capacity to 3 elements   

//SetLength(wishes, 3);   // Set the capacity to 3 elements

 

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

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

end;

Result is:

----

0

2

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

Note: Here we have defined a wishes array containing string elements. We use the SetLength routine to set the array size. Such arrays are called dynamic because their size is determined dynamically (at run time). The SetLength routine can be used to change the array size more than once - decresaing or increasing the size as desired. My wishes array (list) may indeed grow quite large over time.
Note that we have not given the starting index of the array. This is because we cannot - dynamic arrays always start at index 0.

 

* Using SetLength(wishes, 3), Pas2JS will give your an error : "Expected type String instead of Array of String".

 

 

JS output:

function W3Button2Click(Self, Sender$5) {

      var wishes = [];

      $ArraySetLen(wishes,3,"");

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

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

   }

 

function $ArraySetLen(a,n,v) {

       var o=a.length;

       if (o==n) return;

       if (o>n) a.length=n; else for (;o<n;o++) a.push(v);

};