diff --git a/demo/extend_jsclass/ExtendJSFunctionClass1.html b/demo/extend_jsclass/ExtendJSFunctionClass1.html new file mode 100644 index 0000000..34e17ff --- /dev/null +++ b/demo/extend_jsclass/ExtendJSFunctionClass1.html @@ -0,0 +1,28 @@ + + + + + + Demo - descend Pascal class from JS function + + + + + + + + diff --git a/demo/extend_jsclass/ExtendJSFunctionClass1.lpi b/demo/extend_jsclass/ExtendJSFunctionClass1.lpi new file mode 100644 index 0000000..2b23be0 --- /dev/null +++ b/demo/extend_jsclass/ExtendJSFunctionClass1.lpi @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + <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> diff --git a/demo/extend_jsclass/ExtendJSFunctionClass1.lpr b/demo/extend_jsclass/ExtendJSFunctionClass1.lpr new file mode 100644 index 0000000..978858b --- /dev/null +++ b/demo/extend_jsclass/ExtendJSFunctionClass1.lpr @@ -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.