简体   繁体   中英

Reconstructing code from closure compiler

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:

    • Compressed code of the well working file
    • Compressed code of the bad working file
    • Uncompressed code of the bad working file

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.

看起来区别是,坏的人在做“相同的东西”之前正在等待B加载,而好人则立即这样做。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM