I have some HTML that contains a JSON string. In the on DOM ready callback, I have something like this:
MyObject = JSON.parse($('#TheJsonString').html());
Later in my code, I write something this:
var SomeVar = MyObject.MyProp1;
And then when I run the code through the Google closure compiler, I get the warning
Property MyProp1 never defined on MyObject.
How should the code be written so that it doesn’t generate a warning?
The cleanest way to remove the warning is by defining the structure of the JSON. This can be done using the
@typetag:Where
MyProp1is the name of the property, andstringis the type.Google’s Closure compiler will rename the variable. If you don’t want that, you have to use quotes + brackets instead of the dot-notation:
Example: paste the following in the Closure Compiler:
Output: