我有一个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/