简体   繁体   中英

Underscore.js externs for Google Closure Compiler

I am using Luke Rodgers' Backbone and Underscore externs but cannot work out why _.extend is being modified in the compiled output.

For example in Backbone 0.9.2 the following code (193:195):

if (defaults = getValue(this, 'defaults')) {
  attributes = _.extend({}, defaults, attributes);
}

is compiled to:

if(c=r(this,"defaults"))a=f.extend({},c,a);

I used the following compile command:

java -jar compiler.jar 
     --externs backbone-0.9.2-externs.js 
     --externs underscore-1.3.3-externs.js 
     --compilation_level ADVANCED_OPTIMIZATIONS 
     --js underscore-1.3.3.js  
     --js backbone-0.9.2.js 
     --js_output_file compiled.js

If I include another JavaScript file:

     --js test.js

that contains the following:

_.extend({}, {name: 'test'});

it is unmodified in compiled.js .

Does anyone know why calls to _.extend in backbone.js are being modified?

Glad you've found my externs files useful. Off the top of my head, I believe the issue you are encountering is due to the fact that you are both using externs files AND compiling the libraries.

Typically externs files are used when the library is not part of the compilation procedure. Some people may just use pre-compiled versions of (eg) jQuery, Underscore, etc., or link to the shared Google libraries to take advantage of pre-cached js, yet still want to make Closure Compiler understand the jQuery and Underscore API, and so they use externs.

If what you're looking for is the explicit type annotations in the Underscore and Backbone libraries, this is a somewhat different problem.

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