防止闭包重命名对象键

Prevent closure to from renaming object keys

我在代码的其他地方定义了一个 document['key']。基本上是客户端在 html <script></script> 标签中设置的 API 键,就像这样。

<script type="text/javascript">
(function(){
  document.clientKey = 'a uuid unique to client';
})();
</script>

我通过 document['clientKey'] 在我的 javascript 文件中调用了这个密钥并且它工作正常,直到我将它传递给闭包,闭包将它重命名为 document.I。我可以强制关闭以保留字符串 'clientKey'

Understanding the Restrictions Imposed by the Closure Compiler

中对此进行了介绍

Using string names to refer to object properties:

The Compiler renames properties in Advanced mode, but it never renames strings. If you need to refer to a property with a quoted string, always use a quoted string

var x = { 'unrenamed_property': 1 };
x['unrenamed_property'];  // This is OK.
if ( 'unrenamed_property' in x ) {};   // This is OK