1

I have an array of objects that I generate from JSON.parse. I access its properties like this:

AnObject['PhoneList'][i]['PhoneLabel']

When I run the code through the google closure compiler, the name of the properties is not obfuscated and clearly visible. Why are the names of object properties not obfuscated?

frenchie
  • 51,731
  • 109
  • 304
  • 510
  • `MyArray[i]MyObject['Prop1']` is not valid javascript. Please correct. Do you perhaps means `MyArray[i].MyObject['Prop1']`? – jfriend00 Mar 07 '12 at 23:05
  • @jfriend00: ok, made a correction. It's actually an object that contains an array of other objects. – frenchie Mar 07 '12 at 23:16
  • You are using strings: ['PhoneList'] and ['PhoneLabel']. The Closure Compiler never renames strings. – Johann Jul 18 '17 at 15:59

3 Answers3

2

Google closure has a difficult time figuring out what it can and can't rename. For example, any data structure that is created or referenced by code outside the Google closure compiled code cannot be renamed or the two sides won't be expecting the same code. Further, referencing properties via constructed strings makes it next to impossible for closure to do it's job well. As such, Closure has a whole bunch of rules and settings to help you control this and direct it for what to do. I'd suggest you read up on those rules/settings at these references:

https://developers.google.com/closure/compiler/docs/api-tutorial3

https://developers.google.com/closure/compiler/docs/compilation_levels

https://groups.google.com/group/closure-stylesheets-discuss/browse_thread/thread/386ba6db27a43887?pli=1

https://developers.google.com/closure/compiler/docs/limitations

And, quoted from this last reference:

String representations of function or parameter names:

The Compiler renames functions and function parameters but does not change any strings in your code that refer to functions or parameters by name. You should thus avoid representing function or parameter names as strings in your code. For example, the Prototype library function argumentNames() uses Function.toString() to retrieve the names of a function's parameters. But while argumentNames() might tempt you to use the names of arguments in your code, Simple mode compilation breaks this kind of reference.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I changed from dot notation to bracket notation because I was getting warning that properties weren't defined. http://stackoverflow.com/questions/9561138/how-can-i-prevent-the-warning-property-myprop1-never-defined-on-myobject – frenchie Mar 07 '12 at 23:28
  • @frenchie - Well, that's keeping closure from being able to do it's thing on those properties. I've already said my piece to you multiple times about what you're doing in that other question. No compiler/obscuration/compression tool cannot succesfully rename properties that are generated by an outside agency (outside of the compile scope). That appears to be what you're doing or wanting to do. You're loading JSON from some HTML (which is outside the scope of Closure) - therefore those properties cannot be renamed by closure or your code won't match the JSON. – jfriend00 Mar 07 '12 at 23:31
  • I'm still very confused about the issue. The json string contains appointment data for a certain day. When the page loads, the HTML contains the data for the current day. Then, the user can also load data for another via ajax. It's the exact same object either way and the function starts by parsing the json string; regardless of where it comes from HTML or ajax. How do I write the function that takes the json string and parses it so that the closure compiler work? – frenchie Mar 08 '12 at 11:47
  • @frenchie - If the JSON string originates on your server (either via ajax or via the initial page), then you cannot allow renaming of the properties in that JSON string because even if your client code renames all references to them, the server code will still be generating the original/longer property names. So, it is a good idea to reference those property names via string references ['propName'] so the closure compiler will not mess with them. There are some other options given in some of the references I quoted in my answer, but all answers have to prevent client-side renaming for those. – jfriend00 Mar 08 '12 at 12:40
  • ok, thanks; you're right that since I use the same object property name for server json deserialization, it's actually a good thing not to rename those. I'll leave my objects in bracket notations. – frenchie Mar 08 '12 at 12:43
0

Google Closure Compiler doesn't touch quoted strings.

myObj.prop = 3;

will result in prop being renamed (and myObj too perhaps)

myObj['prop'] = 3;

will result is 'prop' remaining untouched.

Paolo
  • 15,233
  • 27
  • 70
  • 91
0

Quoted strings is the syntax used for properties that should not be renamed ("exported"). you probably want to use

AnObject.PhoneList[i].PhoneLabel

instead.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194