webcore js objects in form class

I'm playing with the webaudioapi and when I define any of the objects in the form's class, I can compile them ok, but if I try creating them in, say, WebFormCreate, I get an error saying they're undefined.

I get the same error if I define them at a global scope.

It only works if I define them locally inside of the method that uses them.

I can't see this as something specific to this library because they're defined as Delphi classes.

It is odd that they have to be created with the js .new() function rather than a normal .Create call.

What am I missing?

unit test1
. . .
  TForm1 = class(TWebForm)
  . . .
  private
    ctx: TJSAudioContext;
  . . .
  end;

. . .

procedure TForm1.WebFormCreate(sender: TObject);
begin
    asm
      ctx = new (window.AudioContext || window.webkitAudioContext)();
    end;
   . . .
end;

When the web form is created, I get this error:

ERROR
Uncaught ReferenceError: ctx is not defined | ReferenceError: ctx is not defined at Object.WebFormCreate (http://localhost:8000/AudioTest1/AudioTest1.js:45948:11) at Object.cb (http://localhost:8000/AudioTest1/AudioTest1.js:242:19) at Object.DoCreate (http://localhost:8000/AudioTest1/AudioTest1.js:36007:40) at Object.Create$5 (http://localhost:8000/AudioTest1/AudioTest1.js:36432:12) at Object.c.$create (http://localhost:8000/AudioTest1/AudioTest1.js:328:19) at DoStatusCreate (http://localhost:8000/AudioTest1/AudioTest1.js:37584:32) at XMLHttpRequest.cb (http://localhost:8000/AudioTest1/AudioTest1.js:256:21)
at http://localhost:8000/AudioTest1/AudioTest1.js [259:48]


but if I move the declaration down into the method:

procedure TForm1.WebFormCreate(Sender: TObject);
var
    ctx: TJSAudioContext;
. . .

I get no error when I run the form. But that means I cannot access these vars from other methods in the form.

Form variables in the context of an ASM block are referenced as this.VARIABLE

i.e. write your code as

asm
      this.ctx = new (window.AudioContext || window.webkitAudioContext)();
end;

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.