-1

I prepare three files:

java -jar compiler.jar --js=utf8.js --js_output_file=utf8-advanced.js --charset=utf8 --compilation_level=ADVANCED_OPTIMIZATIONS --formatting=SINGLE_QUOTES --formatting=PRETTY_PRINT --externs=externs.js --use_only_custom_externs

Javascript:

window.erest['module'] = window.erest['module'] || {};
window.erest.module = window.erest['module'];

var module = window.erest.module;

window.erest.module.hello = function() {
    for (var i = 0; i < 5; i++) {
        window.erest.debug.hello();
    }
}

window.erest.module.max = function() {
    var list = [];
    for (var i = 0; i < list.length; i++) {
        window.erest.debug.hello();
    }
}

window.erest.module.maximum = function() {
    window.erest.debug.hello();
}

Externs:

/**
 * @externs
 */

/**
 * @noalias
 */
var erest = {};

erest.debug = {};

/**
 * @return null
 */
erest.debug.hello = function () {};

Output:

window.a.module = window.a.module || {};
window.a.b = window.a.module;
window.a.b.hello = function() {
  for (var b = 0;5 > b;b++) {
    window.a.debug.hello();
  }
};
window.a.b.max = function() {
  for (var b = [], c = 0;c < b.length;c++) {
    window.a.debug.hello();
  }
};
window.a.b.c = function() {
  window.a.debug.hello();
};

Question - why externs not works here:

window.a.debug.hello() should be window.erest.debug.hello()

Why this extern not help:

var erest = {};
Chameleon
  • 9,722
  • 16
  • 65
  • 127

1 Answers1

1

Closure-compiler sees properties on the global object as different objects than global variables. You must reference a name consistently.

See the "Referring to variables as properties of the global object" bullet under Understanding the Restrictions Imposed by the Closure Compiler

Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57
  • I found that advanced optimization is not good for me - much more codding and easy to make bugs so it require much more testing/$. Simple optimization is enough to little complicate/strip code for reading and reduce size. – Chameleon Nov 21 '13 at 07:25