javascript - 防止闭包重命名对象键

标签 javascript google-closure-compiler

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

<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

关于javascript - 防止闭包重命名对象键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29354192/

相关文章:

javascript - 任何应用程序都无法执行此选项 - 输入类型 ="file"

javascript - 在 knockout 的 foreach 循环中使用 $data 作为条件?

javascript - 如何指示闭包编译器不重命名/删除局部变量?

javascript - 闭包编译器 : could not determine the type of this expression at callback

javascript - 调用多个函数 'onClick'

javascript - JS 上的 JSON 回复解析

javascript - 记录和保存音频文件变得很奇怪

javascript - Closure 编译器认为参数不匹配是由于从未发生过的 null 条件

用于访问 Closure Compiler Service API 的 Python 库

javascript - 使用 Google Closure Templates 重写一个名为 "default"的属性