declare variable in html
I am not very good at coding but i really need help here is an example of what i am talking about
ok so i am making a game in p5.play and in order to define a sprite you need to us
var l = createSprite(200,200);
i want to automate this using a variable
var count = 5;
var l + count = 1;
var l + count = 2;
var l + "foo" = 40
lfoo is undefined , it always returns an error is this impossible? if so how can i automate defining variables?
You can't define variables like this. You have to define it through writing the absolute name for them. But to call them you can use window["l"+"foo"].
var l = 40; var lfoo = 30; console.log(window["l"]+"...."+window["l"+"foo"]);
You could use window to declare global variables
var str = 'l' + 'foo'; window[str] = 40; //Then you can access lfoo directly
- what do you want in result ?
- Why do you want that? You can create the new variable directly with
var lfoo = 40.
- you can't define variables like this. you have to define it through writing the absolute name for them. But to call them you can use window["l"+"foo"]
- Why do you need that? To me it smells like bad code..