mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-13 13:59:07 +02:00
pas2js: doc about extending JS function
git-svn-id: trunk@45715 -
This commit is contained in:
parent
8e7a51065f
commit
cbf5f1a769
@ -2799,10 +2799,11 @@ End.
|
|||||||
|
|
||||||
<div class="section">
|
<div class="section">
|
||||||
<h2 id="externalclassancestor">External class as ancestor</h2>
|
<h2 id="externalclassancestor">External class as ancestor</h2>
|
||||||
A Pascal class can descend from an external class.<br>
|
A Pascal class can descend from an external class - a JS object or function.<br>
|
||||||
The methods <i>AfterConstruction</i> and <i>BeforeDestruction</i>
|
The methods <i>AfterConstruction</i> and <i>BeforeDestruction</i>
|
||||||
are called if they exist.<br>
|
are called if they exist.<br>
|
||||||
New instances are created by default with <i>Object.create(ancestorclass)</i>.<br>
|
New instances of a JS Object descendant are created by default with <i>Object.create(ancestorclass)</i>.<br>
|
||||||
|
New instances of a JS Function descendant are created by default with <i>new DescendantFunc()</i>.<br>
|
||||||
You can override this, by providing a<br>
|
You can override this, by providing a<br>
|
||||||
<b>class function NewInstance(fnname: string; const paramsarray): TPasClass; virtual;</b>.
|
<b>class function NewInstance(fnname: string; const paramsarray): TPasClass; virtual;</b>.
|
||||||
This method is called to create a new instance and before calling the constructor.
|
This method is called to create a new instance and before calling the constructor.
|
||||||
@ -2817,19 +2818,22 @@ End.
|
|||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<pre>Program MyModule;
|
<pre>
|
||||||
|
// Example for descending a Pascal class from a JS Object
|
||||||
|
Program MyModule;
|
||||||
{$modeswitch externalclass}
|
{$modeswitch externalclass}
|
||||||
type
|
type
|
||||||
TExtA = class external name 'ExtA'
|
TExtA = class external name 'ExtA'
|
||||||
end;
|
end;
|
||||||
TMyB = class(TExtA)
|
TMyB = class(TExtA)
|
||||||
protected
|
protected
|
||||||
|
// optional: override default allocation
|
||||||
class function NewInstance(fnname: string; const paramarray): TMyB; virtual;
|
class function NewInstance(fnname: string; const paramarray): TMyB; virtual;
|
||||||
end;
|
end;
|
||||||
class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;
|
class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;
|
||||||
Begin
|
Begin
|
||||||
asm
|
asm
|
||||||
Result = Object.create(ExtA);
|
Result = Object.create(ExtA); // that is what the rtl does
|
||||||
end;
|
end;
|
||||||
End;
|
End;
|
||||||
|
|
||||||
@ -2854,6 +2858,55 @@ End.
|
|||||||
$mod.$main = function () {
|
$mod.$main = function () {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
</pre>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<table class="sample">
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<th>Pascal</th>
|
||||||
|
<th>JavaScript</th>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<pre>
|
||||||
|
// Example for descending a Pascal class from a JS Function
|
||||||
|
Program MyModule;
|
||||||
|
{$modeswitch externalclass}
|
||||||
|
uses JS;
|
||||||
|
type
|
||||||
|
TExternalFunc = class external name 'ExternalFunc'(TJSFunction)
|
||||||
|
constructor New(a: word);
|
||||||
|
end;
|
||||||
|
TMyFunc = class(TExternalFunc)
|
||||||
|
constructor Create(b: word);
|
||||||
|
end;
|
||||||
|
constructor TMyFunc.Create(b: word);
|
||||||
|
Begin
|
||||||
|
inherited New(b+1); // optional: call inherited constructor function
|
||||||
|
End;
|
||||||
|
|
||||||
|
Begin
|
||||||
|
End.
|
||||||
|
</pre>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<pre>rtl.module("program",["System","js"],function () {
|
||||||
|
var $mod = this;
|
||||||
|
rtl.createClassExt($mod, "TMyFunc", ExternalFunc, "", function () {
|
||||||
|
this.$init = function () {
|
||||||
|
};
|
||||||
|
this.$final = function () {
|
||||||
|
};
|
||||||
|
this.Create$2 = function (b) {
|
||||||
|
this.$ancestorfunc(b+1);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
$mod.$main = function () {
|
||||||
|
};
|
||||||
|
});
|
||||||
</pre>
|
</pre>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
Loading…
Reference in New Issue
Block a user