The term initializing means to assign some value to the variable.
Basically, the actual use of variables comes under the initialization part. In pas2js each data type has some default value which is used when there is no explicitly set value for a given variable.
Compile Time Initialization: It means to provide the value to the variable during the compilation of the program. If the programmer didn’t provide any value then the compiler will provide some default value to the variables in some cases.
Variables are converted without type, because JavaScript lacks a clear type. They are however always initialized, which helps JavaScript engines to optimize.
PAS2JS NOTES:
Type casting a boolean to integer, gives 0 for false and 1 for true.
Type casting an integer to boolean, gives false for 0 and true otherwise.
A char is translated to a JS string, because JS lacks a native char type.
A char is a single JS char code. An UTF-16 codepoint can contain one or two char.
Integers overflows at runtime differ from Delphi/FPC, due to the double format.
For example adding var i: byte = 200; ... i:=i+100; will result in
i=300 instead of i=44 as in Delphi/FPC.
When range checking {$R+} is enabled i:=300 will raise an ERangeError.
type cast integer to integer, e.g. byte(aLongInt)
with range checking enabled: error if outside range
without range checking: emulates the FPC/Delphi behaviour:
e.g. byte(value) translates to value & 0xff,
shortint(value) translates to value & 0xff <<24 >> 24.
The mod-operator works 32-bit signed in JS.
Strings are translated to JavaScript strings. They are initialized with ""
and are never null.
There are no ShortString, AnsiString or RawByteString.
Unicodestring and Widestring are alias of String.
JavaScript strings are immutable, which means
that changing a single character in a string, creates a new string. So a s[2]:='c';
is a slow operation in pas2js compared to Delphi/FPC.
_____ Although pas2js creates .js files encoded as UTF-8 with BOM, JavaScript strings are
UTF-16 at runtime. Keep in mind that one UTF-16 codepoint can need two char,
and a visible glyph can need several codepoints. Same as in Delphi.
Pas2JS pascal source code
{ filename: project1.lpr }
program project1;
{$mode objfpc}
uses
Classes, SysUtils, JS;
type
TForm = class
end;
const
c1: integer=3;
c2 = 'abc';
c3 = 234;
c4 = 12.45;
c5 = nil;
var
v1: string;
v2,v3: double;
v4 :byte=0;
v5 :TForm;
v6 :TIdentMapEntry;
v7 :string='abcäöü';
v8 :char='c';
v9 :array of byte;
begin
// Your code here
end.