1

Why does the Closure Compiler generate different code (using advance option) for the following two functions:

  var function1 = function() {
        return 1 * Math.random();
  };

  window['function1'] = function1;  // export function1

  var function2 = function() {
        return function1() + 1;
  };

  window['function2'] = function2;  // export function2

This is the code generated:

  function a() {
        return 1 * Math.random();
  }

  window.function1 = a;

  window.function2 = function() {
        return a() + 1;  // call to a() fails in a more complex example
  };

Notice that function1 has been renamed to a and a is assigned to the global varible function1. With function2 there is no other variable name associated with it. Why?

The reason why I ask is, in the case with my code, the call to function1 from function2 fails because the renamed function1 is not seen as a function call in function2 but rather the Javascript interpreter thinks that a() is a number.

Any insight is appreciated. TIA.

3
  • That's what minifiers do. It's called function name mangling, you can probably find it on Google. I use uglifyjs not closure, but there should be an option to turn that feature off. The advance feature AFAIK can cause problems if you don't write your code for closure. Commented Nov 14, 2012 at 0:07
  • @elclanrs (1) the export is to keep the names from being mangled and (2) either way, why isn't the syntax created consistent? Commented Nov 14, 2012 at 0:09
  • No idea, like I said I use uglifyjs to minify/optimize code. You could try it out, it's pretty fast and it's written in JS. Commented Nov 14, 2012 at 0:15

2 Answers 2

2

If "a" is a number, most likely it is getting overwritten by something else. If you aren't using multiple modules, try using the output wrapper option to isolate the globals (There are other options to isolate globals if you are using multiple modules). Often defining a setter on "windows.a" and setting a break point there will let you know how this overwrite is happening.

Sign up to request clarification or add additional context in comments.

1 Comment

I am using multiple modules and a was being overwritten when an uncompiled js file was mistakenly loaded rather than the compiled version, i.e. stupid mistake.
2

Using ADVANCED_OPTIMIZATIONS, Closure Compiler will remove unused code. Since the only use of function2 was in the export, the assignment was made directly to the exported name (rather than first to a variable). function1 was both exported and used by function2 so the compiler left the initial named function as it was referenced twice.

As for the failure, we'd need to see your actual code to explain what's happening.

1 Comment

Up vote. Thanks for answering why Closure genererates the different function declarations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.