简体   繁体   中英

Google Closure Compiler, some properties will never be renamed?

today I've found an issue related with property rename by the Google Closure Compiler. Would be happy if somebody could explain me why this code:

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// ==/ClosureCompiler==

// ADD YOUR CODE HERE


console.info({
  foo: 'bar',
  index: 10,
  bla: 'x'
});

always compiles to:

console.info({b:"bar",index:10,a:"x"});

property "index" will never be renamed to some different name. The question is why? And how to force it's rename?

Generally, the Closure Compiler can rename based on types. However, it does back off in the face of loose types. Object literals that are not directly assigned to prototype properties are one of those loose types. Here it is not renamed because an "index" property exists in the standard externs. There are several definition but here is one:

https://github.com/google/closure-compiler/blob/641cf61b99280b8bb4b62ecce873e51208501fec/externs/es3.js#L718

只是没有时间了解这个“非常聪明的逻辑”,我只是将此属性重命名为其他对象未使用的其他属性。

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