简体   繁体   中英

Prevent closure to from renaming object keys

I have a document['key'] that i am defining in elsewhere in my code. Basically an API key that is being set by client in html <script></script> tag like this.

<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 . Can I force closure to keep the string '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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM