Please vote, tell us what you think!
I'm also interested in the use-case of PAS2JS. Why use Pascal instead of JavaScript (with its ecosystem)?Mainly because Pascal is strongly typed as opposed to JavaScript: that means that the transpiler can generate more accurate code, less prone to errors, even if it outputs eventually JavaScript.
What lacks are two features, one of which I deem important:that is anonymous methods.
But that would be for FPC itself to have first.
There isn't any reason why Pas2JS couldn't have anonymous functions before FPC.Well,
In the wiki I've read that it has support to generate nodejs modules. But does it allow to interact with existing modules? (the Haxe language for example does this with 'externs' - you describe the API to an external JavaScript library / module, so the compiler does know about the external structure)
Same in pas2js. It supports external classes, functions and variables. See unit js for examples.Wow, good to know! So it seems it is now time to have a deep look into PAS2JS :-) Thanks for the hint!
You can even descend a new Pascal class from an external JS class.
And you can use asm-blocks to insert JS directly. But the external classes are so powerful, that even the RTL hardly use asm blocks.
@mattias
Can I use this also in native Free Pascal code? I've tried your interesting closure example, but in my version the value of y is initialized to the same value as x at every call:
Note that pas2js supports closures, so for example instead of using an anymous function you can use a sub function:
Delphi:
function MakeAdder(y: Integer): TFuncOfInt; begin Result := function(x: Integer) begin Result := x + y; end; end;
Pas2js:
function MakeAdder(y: Integer): TFuncOfInt; function Adder(x: Integer): integer; begin Result := x + y; end; begin Result:=@Adder; end;
I just can not just believe, anonymous functions is not at top of the list.Can't add something better to say!
It's so boring writing sub functions :( to do simple things.