mirror of
https://gitlab.com/freepascal.org/fpc/pas2js.git
synced 2025-08-22 15:19:15 +02:00
demo: descend Pascal class from JS function
This commit is contained in:
parent
3df6637e4b
commit
271f20ea13
28
demo/extend_jsclass/ExtendJSFunctionClass1.html
Normal file
28
demo/extend_jsclass/ExtendJSFunctionClass1.html
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>Demo - descend Pascal class from JS function</title>
|
||||||
|
<script type="text/javascript">
|
||||||
|
|
||||||
|
function Person(first, last, age) {
|
||||||
|
console.log('Person first='+first+' last='+last+' age='+age);
|
||||||
|
this.first = first;
|
||||||
|
this.last = last;
|
||||||
|
this.age = age;
|
||||||
|
this.run = function(value){
|
||||||
|
console.log('Person.run value='+value);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
<script src="ExtendJSFunctionClass1.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<script>
|
||||||
|
rtl.run();
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
90
demo/extend_jsclass/ExtendJSFunctionClass1.lpi
Normal file
90
demo/extend_jsclass/ExtendJSFunctionClass1.lpi
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<CONFIG>
|
||||||
|
<ProjectOptions>
|
||||||
|
<Version Value="12"/>
|
||||||
|
<General>
|
||||||
|
<Flags>
|
||||||
|
<MainUnitHasCreateFormStatements Value="False"/>
|
||||||
|
<MainUnitHasTitleStatement Value="False"/>
|
||||||
|
<MainUnitHasScaledStatement Value="False"/>
|
||||||
|
<Runnable Value="False"/>
|
||||||
|
</Flags>
|
||||||
|
<SessionStorage Value="InProjectDir"/>
|
||||||
|
<Title Value="ExtendJSFunctionClass1"/>
|
||||||
|
<UseAppBundle Value="False"/>
|
||||||
|
<ResourceType Value="res"/>
|
||||||
|
</General>
|
||||||
|
<CustomData Count="4">
|
||||||
|
<Item0 Name="MaintainHTML" Value="1"/>
|
||||||
|
<Item1 Name="PasJSHTMLFile" Value="project1.html"/>
|
||||||
|
<Item2 Name="PasJSPort" Value="0"/>
|
||||||
|
<Item3 Name="PasJSWebBrowserProject" Value="1"/>
|
||||||
|
</CustomData>
|
||||||
|
<BuildModes>
|
||||||
|
<Item Name="Default" Default="True"/>
|
||||||
|
</BuildModes>
|
||||||
|
<PublishOptions>
|
||||||
|
<Version Value="2"/>
|
||||||
|
<UseFileFilters Value="True"/>
|
||||||
|
</PublishOptions>
|
||||||
|
<RunParams>
|
||||||
|
<FormatVersion Value="2"/>
|
||||||
|
</RunParams>
|
||||||
|
<Units>
|
||||||
|
<Unit>
|
||||||
|
<Filename Value="ExtendJSFunctionClass1.lpr"/>
|
||||||
|
<IsPartOfProject Value="True"/>
|
||||||
|
</Unit>
|
||||||
|
<Unit>
|
||||||
|
<Filename Value="ExtendJSFunctionClass1.html"/>
|
||||||
|
<IsPartOfProject Value="True"/>
|
||||||
|
<CustomData Count="1">
|
||||||
|
<Item0 Name="PasJSIsProjectHTMLFile" Value="1"/>
|
||||||
|
</CustomData>
|
||||||
|
</Unit>
|
||||||
|
</Units>
|
||||||
|
</ProjectOptions>
|
||||||
|
<CompilerOptions>
|
||||||
|
<Version Value="11"/>
|
||||||
|
<Target FileExt=".js">
|
||||||
|
<Filename Value="ExtendJSFunctionClass1"/>
|
||||||
|
</Target>
|
||||||
|
<SearchPaths>
|
||||||
|
<IncludeFiles Value="$(ProjOutDir)"/>
|
||||||
|
<UnitOutputDirectory Value="js"/>
|
||||||
|
</SearchPaths>
|
||||||
|
<Parsing>
|
||||||
|
<SyntaxOptions>
|
||||||
|
<AllowLabel Value="False"/>
|
||||||
|
<CPPInline Value="False"/>
|
||||||
|
<UseAnsiStrings Value="False"/>
|
||||||
|
</SyntaxOptions>
|
||||||
|
</Parsing>
|
||||||
|
<CodeGeneration>
|
||||||
|
<TargetOS Value="browser"/>
|
||||||
|
</CodeGeneration>
|
||||||
|
<Linking>
|
||||||
|
<Debugging>
|
||||||
|
<GenerateDebugInfo Value="False"/>
|
||||||
|
<UseLineInfoUnit Value="False"/>
|
||||||
|
</Debugging>
|
||||||
|
</Linking>
|
||||||
|
<Other>
|
||||||
|
<CustomOptions Value="-Jeutf-8 -Jirtl.js -Jc -Jminclude"/>
|
||||||
|
<CompilerPath Value="$(pas2js)"/>
|
||||||
|
</Other>
|
||||||
|
</CompilerOptions>
|
||||||
|
<Debugging>
|
||||||
|
<Exceptions>
|
||||||
|
<Item>
|
||||||
|
<Name Value="EAbort"/>
|
||||||
|
</Item>
|
||||||
|
<Item>
|
||||||
|
<Name Value="ECodetoolError"/>
|
||||||
|
</Item>
|
||||||
|
<Item>
|
||||||
|
<Name Value="EFOpenError"/>
|
||||||
|
</Item>
|
||||||
|
</Exceptions>
|
||||||
|
</Debugging>
|
||||||
|
</CONFIG>
|
61
demo/extend_jsclass/ExtendJSFunctionClass1.lpr
Normal file
61
demo/extend_jsclass/ExtendJSFunctionClass1.lpr
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
program ExtendJSFunctionClass1;
|
||||||
|
|
||||||
|
{$mode objfpc}
|
||||||
|
{$ModeSwitch externalclass}
|
||||||
|
|
||||||
|
uses
|
||||||
|
JS, Classes, SysUtils, Web;
|
||||||
|
|
||||||
|
type
|
||||||
|
|
||||||
|
{ TJSPerson - a JS function, which is used like a class }
|
||||||
|
|
||||||
|
TJSPerson = class external name 'Person'(TJSFunction)
|
||||||
|
public
|
||||||
|
First: string; external name 'first';
|
||||||
|
Last: string; external name 'last';
|
||||||
|
Age: nativeint; external name 'age';
|
||||||
|
constructor New(aFirst, aLast: string; aAge: nativeint);
|
||||||
|
procedure Run(Value: string); external name 'run';
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ TContact - descend a Pascal class from the JS function }
|
||||||
|
|
||||||
|
TContact = class(TJSPerson)
|
||||||
|
public
|
||||||
|
EMail: string;
|
||||||
|
constructor Create(aFirst, aLast: string; aAge: nativeint; aEMail: string); reintroduce;
|
||||||
|
end;
|
||||||
|
|
||||||
|
constructor TContact.Create(aFirst, aLast: string; aAge: nativeint;
|
||||||
|
aEMail: string);
|
||||||
|
begin
|
||||||
|
inherited New(aFirst,aLast,aAge);
|
||||||
|
EMail:=aEMail;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TestContact;
|
||||||
|
var
|
||||||
|
aPerson: TJSPerson;
|
||||||
|
aContact: TContact;
|
||||||
|
begin
|
||||||
|
aPerson:=TJSPerson.new('Joe','Smith',23);
|
||||||
|
writeln('TestContact aPerson.First=',aPerson.First,' Last=',aPerson.Last,' Age=',aPerson.Age);
|
||||||
|
// the JS instanceof operator works on the JS object (should be true)
|
||||||
|
writeln('TestContact: aPerson instanceof Person = ',jsInstanceOf(aPerson,TJSPerson));
|
||||||
|
aPerson.Run('TestContact calling aPerson.run');
|
||||||
|
|
||||||
|
aContact:=TContact.Create('Foo','Bar',7,'a@b');
|
||||||
|
writeln('TestContact: aContact.First=',aContact.First);
|
||||||
|
writeln('TestContact: aContact.Last=',aContact.Last);
|
||||||
|
writeln('TestContact: aContact.Age=',aContact.Age);
|
||||||
|
writeln('TestContact: aContact.EMail=',aContact.EMail);
|
||||||
|
// the JS instanceof operator works on the Pascal object (should be true)
|
||||||
|
writeln('TestContact: aContact instanceof Person = ',jsInstanceOf(aPerson,TJSPerson));
|
||||||
|
|
||||||
|
aContact.Run('TestContact called aContact.run');
|
||||||
|
end;
|
||||||
|
|
||||||
|
begin
|
||||||
|
TestContact;
|
||||||
|
end.
|
Loading…
Reference in New Issue
Block a user