redeclaration of var contador

52 views
Skip to first unread message

Jose M. Lobato

unread,
May 22, 2025, 7:20:46 PMMay 22
to Blockly
When I generate JavaScript code in "javascriptGenerator.workspaceToCode(jsWorkspace)", it redeclares the variables. How do I override this behavior?
var elementoContador, contador, botonDecrementar, botonIncrementar;


let contador = 0;
const elementoContador = document.getElementById('contador');
const botonDecrementar = document.getElementById('decrementar');
const botonIncrementar = document.getElementById('incrementar');
function actualizarContador() {
  elementoContador.textContent = contador;
  if (contador < 0) {
    elementoContador.style.color = 'red';
  } else {
    elementoContador.style.color = 'black';
  }
}
botonDecrementar.addEventListener('click', () => {
    contador--;
    actualizarContador();

});
botonIncrementar.addEventListener('click', () => {
    contador++;
    actualizarContador();

});
actualizarContador();

If I wrote this incorrectly, please excuse me. I'm writing in a language I don't know, and this is my first time asking for help.

Ben Henning

unread,
May 29, 2025, 6:45:28 PMMay 29
to Blockly
Hi,

It would help to have a small, isolated example that produces this sort of situation. Specifically, if you could provide a small sample of Blockly code that sets up an environment and attempts to generate JavaScript, then the JavaScript that has the declaration issues, that would be really helpful. I couldn't find any existing issues reported with this. While we do have a live demo for generating JavaScript, it doesn't support variables which means this specific scenario can't be produced.

Regards,
Ben

Ronald Bourret (xWF)

unread,
May 30, 2025, 12:39:40 PMMay 30
to blo...@googlegroups.com
Hi,

By design, Blockly's JavaScript code generator globally declares the variables created by Blockly's built-in variable blocks. This is where the var declaration at the top of your code comes from. This is primarily because local scoping is too complex for many users of products that use Blockly.

It appears that the block-code generators for your blocks declare variables (the let and const declarations), which conflicts with Blockly's var declaration. Here are some ideas on how to fix your code, from easiest to hardest:

1) Update your block-code generators so that they assign but do not declare variables. That is, remove the let and const keywords. This would allow Blockly to declare your variables globally without conflicts.

2) Use the lexical variables plugin from MIT App Inventor. This adds complexity, but allows you to use locally scoped variables.

3) Fork Blockly's JavaScript generator to remove the line that declares variables globally and make sure you declare all your variables. This is not recommended.

Good luck!

Ronald Bourret
Technical Writer (Provided by Synergis)


--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/blockly/65138512-c787-4700-97ec-fd5cb893864bn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages