For as popular as JS is, I've found it pretty difficult to find good tutorials and resources online for it. I guess I was looking in the wrong places? Google was just failing? My google-fu isn't as good as I think it is? I dunno...
I think this will be a big help and a good resource, so thanks a million.
Because I've just started messing with javascript, I'm mostly focused on games programming. But more generally, it seems hard to find information about how to write well optimized javascript (especially in terms of memory usage). This could be my problem, though. I get confused by callbacks and knowing when I'm creating objects (especially functions) versus when I'm reusing them. Callbacks are just confusing the hell out of me - specifically inside of something like requestAnimationFrame... how do I prevent new functions from being created every time I use it?
I mean, w3schools is awful for anything but a very quick reference, and while the content on MDN seems to be accurate, it's not very exhaustive and doesn't seem to provide much explanation. Whenever I have a problem with matlab or perl I can go to some great websites or just google my problem to find solutions (and often great explanations) quickly.
I initially posted this, but now I'm not sure if this is what you meant:
Don't do this:
requestAnimationFrame(function () {
//a new instance of this function is created every time
})
do this: var efficientFunc = function () {
//this is created only once and is reused
requestAnimationFrame(efficientFunc);
}
requestAnimationFrame(efficientFunc);
Basically avoid writing anonymous functions for callbacks that are going to be callback-ed more than once.Another thing that might help with memory is make sure you don't keep references to things you want garbage-collected.
E.g. if object `A` has a reference to object `B`, `B`'s memory won't be freed even if you never use it again. MDN has a better explanation and example [1]
As for books I'd recommend Effective Javascript by David Herman.
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memo...
http://jqfundamentals.com/chapter/javascript-basics
it gets jquery specific in the end but the start with Javascript basics is very good.
I agree that its though to find things related to memory use and performance - I am pretty bad at that myself :) That said, a rule of thumb is that whenever you evaluate an object literal ( "{...}") or function expression ( "function(){ ... }") then you will will create a new object/function instance that will have to be garbage collected in the future. As already mentioned, you get around this by declaring the function beforehand (assigning it to a variable) and then reusing that reference in the future.
As for code reference, stay away from w3schools - they suck and only dominate search rankings because of aggressive SEO. I recommend sticking to MDN, or, if you are feeling adventurous, checking out the Ecmascript spec (dunno how good these are with the newfangled game stuff though)
For callbacks there are two things I have to say. First of all, some people used to OOP take a while to get used to callbacks instead of classes + method passing, but after you understand whats going on its hard to go back to living without first-class functions. Secondly, if you have complex code with lots of callbacks calling other callbacks then its going to be ugly no matter what you try to do (taming callback hell is a little industry of its own...). Learning about continuation passing style helps understand things a bit better though.
Finally, one thing you didn't mention but that its good to point out just in case: if you dont already know how to use the debugger in your favorite browser yet, do it ASAP. Use console.log instead of alert for when you want to do "printf debugging" and never use document.write unless you really know what you are doing. I see people doing these last two all the time and its kind of sad...
It looks like a wonderful hub of all sorts of information about game development, good job to the person who put this together! Hopefully I can even apply the lessons I learn from all of this generally to other languages as well.
http://wiki.unity3d.com/index.php/UnityScript_versus_JavaScr...