Strict Mode in EcmaScript 5
EcmaScript 262 rev. 5 introduced a “strict mode”1, which gives you much needed, stricter parsing (and removes some of the bad parts know from rev. 3).
InfoQ has a good introdction to how strict mode is intended to work
Firefox nightly2 (tested in 3.7a2pre) now has support for strict mode, which means I will now get early warnings of silly mistakes, long before I even run JSLint on my code.
1 // use of unscoped or undeclared variables will generate exceptions 2 function myFlawedFunction(){ 3 i = 3; // generates an exception 4 return i; 5 } 6 7 function myBetterFunction(){ 8 var i = 3; // does not generate and exception 9 return i; 10 }
To get the full benefit of strict mode without causing too many headaches of having library code evaluated in strict mode, you can wrap your source in an immediate function.
1 // wrap the source in an immediate function 2 // to limit strict mode to only this scope 3 (function(){ 4 "use strict"; 5 // your code here 6 }());
I’ve created a simple test to show if your browser supports strict mode, why not go test it now?.
As far as I’ve been able to determine, none of Firefox’s competitors nightly builds have support for strict mode.
Firefox nightly has just become my new favorite browser for development.
1 John Resig explains strict mode and more
2 Get a Firefox nightly build and start enjoying strict mode today!