[英]Prevent closure to from renaming object keys
I have a document['key']
that i am defining in elsewhere in my code. 我有一个要在代码其他地方定义的
document['key']
。 Basically an API key that is being set by client in html <script></script>
tag like this. 基本上是这样的,客户端在html
<script></script>
标记中设置的API密钥。
<script type="text/javascript">
(function(){
document.clientKey = 'a uuid unique to client';
})();
</script>
i called this key in my javascript file via document['clientKey']
and it was working fine, until i passed it to closure, which renames it to document.I
. 我通过
document['clientKey']
在我的javascript文件中调用了此密钥,它运行良好,直到将其传递给闭包为止,闭包将其重命名为document.I
。 Can I force closure to keep the string 'clientKey'
我可以强制关闭以保留字符串
'clientKey'
This is covered in 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.