Miscellaneous Strict Warnings
Finally, lets take a look at some other odd strict warnings:
Error #1:
- JSMSG_EQUAL_AS_ASSIGN
"test for equality (==) mistyped as assignment (=)?"
We've all seen this one before.
var a = 3, b = 4 if (a = b) { alert("true") }
The above will fire the alert, because it sets a = b
and
returns a
, instead of checking to see if a == b
.
Error #2:
- JSMSG_TRAILING_COMMA
"trailing comma is not legal in ECMA-262 object initializers"
var obj = { m1,m2, }
Note the last comma in the object definition. You don't need it and don't want it.
Error #3:
- JSMSG_BAD_INDIRECT_CALL
"function eval must be called directly, and not by way of a function of another name."
This applies only to the eval()
function, and you can
reproduce it by:
e = eval; e("alert(1)");For obvious reasons I don't think you need to worry about this one.
Error #4:
- JSMSG_USELESS_EXPR
"useless expression"
This one is also fairly rare, but you can see it from time to time if you simply forget to do something with a variable on a line...
var y = function () { var x; x; // throws warning x = 1; } y()
Conclusion
As you can see, strict warnings are really simple stylistic bugs. But fixing them generally leads to better, more robust code and coding practices. So, while you continue to study the science of JavaScript, take the time to check your code for these minor faults. You'll find it helps your coding ethic quite a lot.
- Tutorial introduction (variable declaration warnings)
- Function related strict warnings
- Miscellaneous strict warnings