Page 1 of 1

Javascript const

Posted: Sun Nov 01, 2020 11:09 am
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?

Re: Javascript const

Posted: Sun Nov 01, 2020 12:45 pm
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.

Re: Javascript const

Posted: Mon Nov 02, 2020 3:36 pm
by Andy
Thanks Peter :)

Out of curiosity. What is the transpiled javascript for the code you fixed?

Re: Javascript const

Posted: Mon Nov 02, 2020 3:41 pm
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;
}

Re: Javascript const

Posted: Mon Nov 02, 2020 3:43 pm
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?

Re: Javascript const

Posted: Mon Nov 02, 2020 8:07 pm
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'.

Re: Javascript const

Posted: Tue Nov 03, 2020 7:11 am
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.