and btw I am not that good about javascript and its scoping rules so I tried this
var val=0;
function ExitMenu(args){
E.showMenu();
print("Exiting",val,args);
}
function click(args){
print("Clicked",val,args);
}
function showMenu(){
var val=1;
function click2(args){
print("Clicked",val,args);
}
function click3(args){
click(args);
}
var mainmenu = {
"" : { title : "-- Main Menu --" }, // options
"One" : click,
"Two" : click2,
"Three" : click3,
"four": { title:"Something", value: "x123", format: v => "", onchange:function(v){print("selected",v);setTimeout(ExitMenu,0,v);} },
"Exit" : ExitMenu , // remove the menu
};
E.showMenu(mainmenu);
}
to see what value of val it will print for calling click,click2,click3 and the result is 0,1,0 - click2 takes local variable but click3 when calling click still prints the global variable. I guessed wrong the last case. Now I see that is what lexical scope really means.
BTW what is the function arguments for in the function callback? could I somehow access the menu item so that I know which item was clicked = get its name or even some precomputed value/id I could store there when creating the menu? For now I see some object with "draw' and "scroller" properties, nothing related to clicked item.
The nearest solution is the syntax at line "four" - if the value is string type and I override the format method it shows just title and I get id/value in the onchange. But this is a bit fragile as it depends on the harmless string type of the value for now, if the value is integer or boolean it does more stuff behind scenes.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
and btw I am not that good about javascript and its scoping rules so I tried this
to see what value of
val
it will print for calling click,click2,click3 and the result is 0,1,0 - click2 takes local variable but click3 when calling click still prints the global variable. I guessed wrong the last case. Now I see that is what lexical scope really means.BTW what is the function arguments for in the function callback? could I somehow access the menu item so that I know which item was clicked = get its name or even some precomputed value/id I could store there when creating the menu? For now I see some object with "draw' and "scroller" properties, nothing related to clicked item.
The nearest solution is the syntax at line "four" - if the value is string type and I override the format method it shows just title and I get id/value in the onchange. But this is a bit fragile as it depends on the harmless string type of the value for now, if the value is integer or boolean it does more stuff behind scenes.