I have two different versions of a javascript file: one works fine, the other performs slower. I've tried Closure Compiler on both files but... because I'm an idiot, after a few months I was happy to notice the original source file of the good version was missing. Not only from my hard disk, but even from my memory (I can't remember which changes I've made).
So now I have:
I need to reconstruct the well working source file and, gladly, there are really a few differences between the two compressed version.
Here's where I need your help.
I found this in the well working compressed code
(function () {
// same stuff here
})();
requestAnimFrame(O);
which goes like this in the bad working compressed code
B.onload = function () {
// same stuff here
};
requestAnimFrame(O)
So, how exactly can Closure Compiler declare a function without name? And what are those ();
at the end of the function declaration? How is this realized in uncompressed code? If it might help, this is how it looks like in the uncompressed bad version (which I hope to fix):
function init() {
// same stuff here
}
B.onload = function(){
start()
}
function start() {
init()
requestAnimFrame(0);
}
If you want to test the difference by yourself, here's the result when using the good code and here's the result when using the bad code
try to click the logo: cubes slips faster in the first link, if you have a good cpu it shouldn't get killed when running the visuals. The second link, by contrary, seems to overload the cpu and have a different displays when going on in the animation (I can already fix this second problem having spotted the difference in the code and remembered what I changed).
"So, how exactly can Closure Compiler declare a function without name?"
Because JavaScript supports anonymous functions. You've probably used them quite often when assigning event handlers.
"And what are those
();
at the end of the function declaration?"
They do the same thing they would after any function. They invoke it. Though it's not a function "declaration". Because the function is wrapped in parentheses, it's an anonymous function expression.
"How is this realized in uncompressed code?"
That would depend on the uncompressed code. People use this pattern frequently in their normal code. It creates a local variable scope in which the code can execute.