Pas2JS pascal source code
// ####################################
// TFirstClass
// ####################################
Type
TFirstClass = Class(TObject)
private
Public
Constructor Create; virtual;
Destructor Destroy; override;
End;
Constructor TFirstClass.Create;
Begin
WriteLn('Create ' + Classname);
inherited Create;
end;
Destructor TFirstClass.Destroy;
begin
WriteLn('Destroy ' + Classname);
inherited Destroy;
end;
// ####################################
// TSecondClass
// ####################################
type
TSecondClass = Class(TFirstClass)
private
FTemp: TFirstClass;
public
Constructor Create; Override;
Destructor Destroy; Override;
End;
Constructor TSecondClass.Create;
Begin
WriteLn('2nd Create ' + Classname);
inherited Create;
FTemp := TFirstClass.Create;
end;
Destructor TSecondClass.Destroy;
begin
WriteLn('2nd Destroy ' + Classname);
FTemp.free;
inherited Destroy;
end;
// ####################################
// Test
// ####################################
var First: TFirstClass;
First := TFirstClass.Create;
var Second: TSecondClass;
Second := TSecondClass.Create;
First.free;
Second.free;
{<<< RESULT - CONSOLE LOG >>>
-----------------------------
Errors >>>>
Hint: "Classname" does not match case of declaration ("ClassName") [line: 16, column: 21]
Hint: "Classname" does not match case of declaration ("ClassName") [line: 22, column: 22]
Hint: "Classname" does not match case of declaration ("ClassName") [line: 42, column: 25]
Hint: "Classname" does not match case of declaration ("ClassName") [line: 49, column: 26]
Hint: "free" does not match case of declaration ("Free") [line: 50, column: 9]
Hint: "create" does not match case of declaration ("Create") [line: 63, column: 22]
Hint: "free" does not match case of declaration ("Free") [line: 65, column: 7]
Hint: "free" does not match case of declaration ("Free") [line: 66, column: 8]
Result >>>>
Create TFirstClass
2nd Create TSecondClass
Create TSecondClass
Create TFirstClass
Destroy TFirstClass
2nd Destroy TSecondClass
Destroy TFirstClass
Destroy TSecondClass
-----------------------------
{<<<<<<<<< THE END >>>>>>>>>}
var TObject={
$ClassName: "TObject",
$Parent: null,
ClassName: function (s) { return s.$ClassName },
ClassType: function (s) { return s },
ClassParent: function (s) { return s.$Parent },
$Init: function () {},
Create: function (s) { return s },
Destroy: function (s) { for (var prop in s) if (s.hasOwnProperty(prop)) delete s.prop },
Destroy$: function(s) { return s.ClassType.Destroy(s) },
Free: function (s) { if (s!==null) s.ClassType.Destroy(s) }
}
var Exception={
$ClassName: "Exception",
$Parent: TObject,
$Init: function () { FMessage="" },
Create: function (s,Msg) { s.FMessage=Msg; return s }
}
function $New(c) { var i={ClassType:c}; c.$Init(i); return i }
function $Check(i,z) { if (i) return i; throw Exception.Create($New(Exception),"Object not instantiated"+z); }
function WriteLn(value) {
if (window.console) { window.console.log(value); } };
var TFirstClass = {
$ClassName:"TFirstClass",$Parent:TObject
,$Init:function ($) {
TObject.$Init($);
}
,Create$3:function(Self) {
WriteLn(("Create "+TObject.ClassName($Check(Self,"").ClassType)));
TObject.Create(Self);
return Self
}
,Destroy:function(Self) {
WriteLn(("Destroy "+TObject.ClassName($Check(Self,"").ClassType)));
TObject.Destroy(Self);
}
,Destroy$:function($){return $.ClassType.Destroy($)}
,Create$3$:function($){return $.ClassType.Create$3($)}
};
var TSecondClass = {
$ClassName:"TSecondClass",$Parent:TFirstClass
,$Init:function ($) {
TFirstClass.$Init($);
$.FTemp = null;
}
,Create$3:function(Self) {
WriteLn(("2nd Create "+TObject.ClassName($Check(Self,"").ClassType)));
TFirstClass.Create$3(Self);
$Check(Self,"").FTemp = TFirstClass.Create$3$($New(TFirstClass));
return Self
}
,Destroy:function(Self) {
WriteLn(("2nd Destroy "+TObject.ClassName($Check(Self,"").ClassType)));
TObject.Free($Check(Self,"").FTemp);
TFirstClass.Destroy(Self);
}
,Destroy$:function($){return $.ClassType.Destroy($)}
,Create$3$:function($){return $.ClassType.Create$3($)}
};
var First = null;
var Second = null;
/* <<< main JS >>> */
First = TFirstClass.Create$3$($New(TFirstClass));
Second = TFirstClass.Create$3$($New(TSecondClass));
TObject.Free(First);
TObject.Free(Second);