Javascript const

Using Javascript from SpiderBasic
Andy
Posts: 46
Joined: Sat Feb 15, 2020 5:19 pm
Location: Larnaca, Cyprus
Contact:

Javascript const

Post by Andy »

Code: Select all

Procedure initAudio()
  
  !const AudioContext = window.AudioContext || window.webkitAudioContext
  !const v_audio = new AudioContext()
  
  ProcedureReturn audio
  
EndProcedure
The above code does not work unless i change

Code: Select all

 !const v_audio = new AudioContext()
to

Code: Select all

 !var v_audio = new AudioContext()
Is this normal?
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Javascript const

Post by Peter »

this is the transpiled javascript:

Code: Select all

function f_initaudio() {
 var v_audio=0;
 const AudioContext = window.AudioContext || window.webkitAudioContext
 const v_audio = new AudioContext()
 if (1) return v_audio;
 return 0;
}
As you can see, SpiderBasic has added a

Code: Select all

var v_audio=0;
at the beginning of f_initaudio() because you did not specify it yourself. The corresponding error message is therefore
Uncaught SyntaxError: Identifier 'v_audio' has already been declared
because you try to declare v_audio as const.


This is what the correct function would look like:

Code: Select all

Procedure initAudio()
  Protected audio
  ! const AudioContext = window.AudioContext || window.webkitAudioContext
  ! v_audio = new AudioContext()
  ProcedureReturn audio
EndProcedure
This would not have happened with EnableExplicit. Should always be used.
Andy
Posts: 46
Joined: Sat Feb 15, 2020 5:19 pm
Location: Larnaca, Cyprus
Contact:

Re: Javascript const

Post by Andy »

Thanks Peter :)

Out of curiosity. What is the transpiled javascript for the code you fixed?
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Javascript const

Post by Peter »

Andy wrote:Out of curiosity. What is the transpiled javascript for the code you fixed?
just take a look at your browser developer console ;)

Code: Select all

function f_initaudio() {
 var v_audio=0;
 const AudioContext = window.AudioContext || window.webkitAudioContext
 v_audio = new AudioContext()
 if (1) return v_audio;
 return 0;
}
Andy
Posts: 46
Joined: Sat Feb 15, 2020 5:19 pm
Location: Larnaca, Cyprus
Contact:

Re: Javascript const

Post by Andy »

yes, i just found it in the console. it does not matter that var_audio is not a const? i just have to make sure i dont change the value of this variable?
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Javascript const

Post by Peter »

Andy wrote:yes, i just found it in the console. it does not matter that var_audio is not a const? i just have to make sure i dont change the value of this variable?
the function returns the value of the variable v_audio and not the data type. Even if you write something like this:

Code: Select all

Procedure initAudio()
  ! const AudioContext = window.AudioContext || window.webkitAudioContext
  ! const v_audio = new AudioContext()
  ! return v_audio
EndProcedure
returnValue = initAudio()
... then 'returnValue' is not 'const' but 'var'.
Andy
Posts: 46
Joined: Sat Feb 15, 2020 5:19 pm
Location: Larnaca, Cyprus
Contact:

Re: Javascript const

Post by Andy »

Thanks Peter, i get it now. 'Protected' ensures that the variable is local inside the procedure even if the same variable exists globally so that the return value is always that of the procedure.
Post Reply