Static keyword confusion

Just starting out? Need help? Post your questions and find answers here.
Andy
Posts: 46
Joined: Sat Feb 15, 2020 5:19 pm
Location: Larnaca, Cyprus
Contact:

Static keyword confusion

Post by Andy »

Code: Select all

Procedure ButtonHandler()
  Static value
  
  If value = 0
    value = 20
  EndIf
  
  ! console.log(v_value)
  
EndProcedure

If OpenWindow(0, 0, 0, 300, 440, "Test", #PB_Window_ScreenCentered)
  ButtonGadget(0, 10, 10, 280, 30, "Click Me")
  BindGadgetEvent(0, @ButtonHandler())
EndIf
When I use the Static keywork inside a procedure where inline Javascript needs to access the static variable, Javascript complains that the variable is not defined.
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Static keyword confusion

Post by Peter »

Static variables have a different name in JavaScript:

prefix: "so_" (I assume this stands for static object).
plus: procedure-name
plus: $-sign
plus: variable-name

In your case: so_buttonhandler$v_value

Code: Select all

Procedure ButtonHandler()
  Static value
  
  If value = 0
    value = 20
  EndIf
  
  ! console.log(so_buttonhandler$v_value)
  
EndProcedure
Andy
Posts: 46
Joined: Sat Feb 15, 2020 5:19 pm
Location: Larnaca, Cyprus
Contact:

Re: Static keyword confusion

Post by Andy »

Thanks Peter. Is this stuff documented anywhere?
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Static keyword confusion

Post by Peter »

Is this stuff documented anywhere?
The naming rules are documented here: Inline Javascript.
However, it seems to me that the static variables are missing.
Andy
Posts: 46
Joined: Sat Feb 15, 2020 5:19 pm
Location: Larnaca, Cyprus
Contact:

Re: Static keyword confusion

Post by Andy »

Looks like arrays are also missing
Fred
Site Admin
Posts: 1506
Joined: Mon Feb 24, 2014 10:51 am

Re: Static keyword confusion

Post by Fred »

You can also put a calldebugger and open the webbrowser debugger to inspect the code and see how it's named.
Post Reply