When trying to fail with JSLINT, the following code:
entry.object.var? json_obj.var = entry.object.var: json_obj.var = "";
I get the following error:
Expected An Assignment or Function Call and Instad Saw An Expression
Here I’m trying to do the following: When undefined in Entry.Object.var
assign a value to JSON, or an empty field.
Questions:
- Is it possible to do it more beautiful?
- how to correct this warning? Tried
/ * JSLINT EXPR: True * /
But I get a mistake
undexpected 'Expr'.
Answer 1, Authority 100%
The fact is that the ternary operator returns the value, JSLINT expects that you assign this value of some variable, but this does not happen. Ideologically more correct in such cases not to wise and use if-else.
In your case, it is customary to use something like:
json_obj.var = entry.object.var || "";