gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:27:27 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com