[Pas2js] TJSXMLHttpRequest questions
Mattias Gaertner
nc-gaertnma at netcologne.de
Sun May 20 15:16:34 CEST 2018
On Sun, 20 May 2018 19:16:28 +0700
Ryan Joseph <ryan at thealchemistguild.com> wrote:
> > On May 20, 2018, at 5:51 PM, Mattias Gaertner <nc-gaertnma at netcologne.de> wrote:
> >
> > There are no anonymous methods yet, but there are closures:
> >
> > procedure Test;
> > var
> > xhr : TJSXMLHttpRequest;
> >
> > procedure HandleLoaded;
> > begin
> > if (xhr.readyState = 4) or (xhr.status = 200) then
> > writeln(xhr.responseText);
> > end;
> >
> > begin
> > xhr := TJSXMLHttpRequest.new;
> > xhr.open('GET', 'file.html');
> > xhr.onreadystatechange := @HandleLoaded;
> > xhr.send;
> > end;
>
> I thought closures were what FPC was calling anonymous methods.
And so do most Delphians think. Still wrong.
A closure is a function plus its environment, which implies a garbage
collector.
In JS every function is a closure - anonymous or not.
Theoretically anonymous functions can be closures or normal functions.
In Delphi anonymous functions are always closures, that's why many
Delphians think they are the same.
FPC does not yet have closures, nor anonymous functions. There is an
experimental patch for FPC.
> That’s a pretty important feature for JavaScript.
It is useful in many language.
> That means we need to have a global out in the stack for every time we need to use TJSXMLHttpRequest?
Global? My example above showed a non global function.
> I guess we could rely on a “controller” class or something to make
> this manageable (is that even possible to use a class method?).
> Could subclassing TJSXMLHttpRequest be an option?
You lost me here. Can you give some code?
Mattias
More information about the Pas2js
mailing list