Forum > Pas2JS

Javascript to pas

(1/1)

ramersonw:
Hi,

Hi, I'm trying to use a lib in my pas2js project, but I'm not able to make it work.

I'm triyng to use the snippet below:

--- Code: Javascript  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const myPromisse = MyPromisseInit(); myPromisse.then((data) => {  const temp = new data.SomeConstructor();}) 
My pascal code is as follows:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1; {$mode objfpc}{$modeswitch externalclass} uses  JS; type  TData = class external name 'Object' (TJSObject)  public    constructor SomeConstructor();  end;   function MyPromisseInit(): TJSPromise external name 'MyPromisseInit';  var  test: TJSPromise;begin  test := MyPromisseInit();  test._then(function (data: JSValue): JSValue  var    _data: TData absolute data;    res: JSValue;  begin    res := _data.SomeConstructor();  end);end. 
But it did not work.
Compile says project1.lpr(27,18) Error: External class instance cannot access static constructor SomeConstructor


What I have to change to make it work?

Edited the pascal snippet with complete code.

MarkMLl:

--- Quote from: ramersonw on March 07, 2022, 06:54:43 pm ---What I have to change to make it work?

--- End quote ---

Everything.

You can't nest blocks like that, and in particular you have to declare variables (var declaration) outside the begin/end block that will reference them.

Don't trust the compiler error messages until you've got the layout right.

MarkMLl

marcov:
Actually that is Delphi anonymous methods syntax.

ramersonw:
Yes. It's an anonymous function and It Works with pas2js transpiler.

MarkMLl:
In that case I'd like to apologise for being misleading.

MarkMLl

Navigation

[0] Message Index

Go to full version