The main reason why I ask this question is because I hear about a lot of outdated JS techniques that are no good any more or just outdated. Seeing threads from '08 scares me after hearing all the negativity about older JS.
My main concerns are variables and files.
I want to create some kind of advanced calculator for a video game so I will have TONS of variables that need to be global. Since characters in the game have so many different 'stats', is it better to do something like this even if it means more writing, well just a prefix before everything:
stats.health = 50;
stats.mana = 50;
stats.whatever = 100;
//or is this better:
health = 50;
mana = 50;
whatever = 100;
Next question is about the files. I will have quite a few arrays with tons of data, lots of images loaded and lots of onTouch functions for the images. Do I create seperate JS files for each? First load all the data, then the images and then the functions and at the very bottom before the /html or /body tag add the listeners? I come from Lua, although I've known html + css from before, I'm just starting out in JS so any advices is greatly appreciated.
Another thing that I'm not sure of is, I've seen at least 3 methods of creating images, what's the difference between them?
For example:
for (var i = 0; i < imageSrc.length; i++) {
image[i] = new Image();
image[i].src = "images/" + imageSrc[i] + ".png";
end
also:
document.createElement("img");
That's using javascript to create images while the user is using the page.
Is there a better way to create images that will exist right from the beginning? For example creating this within a div:
<img id="bgMain" src="images/background.png" style="position:absolute; top:0px; left:0px;" />
My last question is: http://jsperf.com/cached-vs-getelementbyid/5
The performance for cache is obviously way better, but I don't understand what obj.elems.el is at all or if I look into it. Does anyone have a link to proper explaining on how to cache variables?
Also any extra info and tips on better coding practices is greatly appreciated. Thanks so much for reading and thanks in advance for any help!