Preventing closure compiler from renaming specific object keys

187 views
Skip to first unread message

Greg Grothaus

unread,
Jun 9, 2016, 1:55:54 PM6/9/16
to Closure Compiler Discuss
I have some JavaScript code which essentially is a small static set of strings which I want to determine membership in during execution, ie:

/*
 * @type {Object<string, number>} *
 */
const TheSet = {
  'foo': 0,
  'bar': 0,
  'baz': 0,
}

...

if (TheSet.hasOwnProperty(someExternalVar)) {
  ...
}


If I access TheSet using hasOwnProperty, closure compiler renames away the string keys, they are no longer present in the final compiled file and the code doesn't work. If I access TheSet using array notation:

if (TheSet.hasOwnProperty[someExternalVar] !== undefined) {
  ...
}

Then the compiler preserves the string keys and all is good. Is there a way to prevent closure compiler from doing this particular renaming? Alternatively is there a better formulation of my whole approach to this problem that would have better performance?

Cheers,
-Greg

John Lenz

unread,
Jun 11, 2016, 4:03:59 PM6/11/16
to closure-compiler

Definitely a bug


--

---
You received this message because you are subscribed to the Google Groups "Closure Compiler Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-compiler-d...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/closure-compiler-discuss/10e40da6-56eb-4c39-a1a8-256e30a76fba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages