Enabling Strict Warnings
Part of writing clean code that doesn’t cause you grief in esoteric browsers is doing the best you can to ensure that your code is completely syntactically valid. My pal Valerio (principle author of Mootools) pointed out how to turn on strict warnings, which I couldn’t get to show up in Firebug, even though I enabled them there.
The problem turned out to be the excellent Web Developer Toolbar which, if you haven’t installed, you should.
The issue is that the default setting in Disable > Javascript > Strict Warnings is to suppress these errors, so even if you enable them in Firebug, you won’t see them. You have to enable them in both places.
Trailing comma is not legal in ECMA-262 object initializers
This error catches me all the time. I’ll start defining the properties of an object and get in the habit of ending each property with a comma, and I’ll leave one on the last argument (or I’ll delete the last argument line, exposing the trailing comma in the previous property). This will crack on IE as soon as you try and run it, though FF is forgiving.
Function does not always return a value
The most common error I found in my code, once I enabled this, was that I had a lot of functions that sometimes returned values, but not always. This is a pretty easy fix; look at the function in question and make sure that it consistently returns something. If you are catching an error condition and returning false to stop the rest of the code from executing, you can either just change the return to return; with no value, or ensure that the function returns something in all other cases. false or true or this or a value.
Assignment to undeclared variable
This happens when you refer to a variable that hasn’t been declared. Browsers are pretty lax about this, but it’s bad practice. I see this a lot in for loops (for(i=0…){…}) - instead of “var i”.
variable hides argument
This happens when you re-declare a variable that’s also an argument (function (arg){var arg = ….}). If you want to overwrite the arg, just do it without the “var”.
Turn those strict warnings on and get cleaning!
about “var i” in loops it is very important coz if you do it without it it actually assign it to window.i(atleast in firefox) and if in the loop you have a function which also have a loop with “i=0;i
nice stuff…. nice article…..
it would defintly help me in the futuer
THANKS
very good advice!
thnx :)