In December of 2009 ECMA-262 v5 was released. That release introduced something called strict mode.
The Strict Variant aka Strict Mode
The official ECMAScript specification defines the Strict Variant as such:
Let's Use It
I'm sure at some point you've seen undefined overridden from what you've expected.
undefined = "foo"; Unfortunately this can happen because it isn't a reserved keyword. Yet at least. With strict mode enabled, doing something like setting undefined would would actually throw an exception. Using it is really simple. It's an opt-in model, based on the scope of which it was placed. To opt in you add the string "use strict". "use strict";
undefined = "foo"; //OH NO YOU DIDN'T!!
Running that in IE10 would actually cause an exception. If you use the F12 Developer Tools and browse to the _Script _tab you will see the following:
SCRIPT5045: Assignment to read only properties not allowed in strict mode
localhost:65035, line 22 character 17 Strict mode is based on the scope of which it was placed.
"use strict" has to be the first statement in that block otherwise it's ignored, comments don't count.
Simple enough right. Now if your browser doesn't support it, then "use strict" is just treated as a string and strict mode is in fact not enabled.
Since implementing strict mode is just a matter of adding the string _"use strict" _nothing should stop you from using it now. BUT before you do, make sure you don't go applying your strict viewpoints on others. As I stated earlier since strict is based on scope you could inadvertently apply strict mode to more than you expected.
Let's use Sprockets10 and look at the result after concatenation:
In this case I managed to disable strict mode based on how the files were merged together. You might in fact impost strict mode on someone OR you could in fact disable it. Your string concatenation result will vary of course but regardless following some best practices will go a lot way here.
Devils in the details...
One of the things I didn't try to cover was all the cases that Strict Mode will check. On the IE Testing Center, they currently have 52 Strict Mode tests listed for you to run in whatever browser you prefer. Further more, you can actually view the source of the test too.
But wait there is more; test262.
- ECMA-262 Language Specification, p. 4, 88, 235 of June 2011 v5.1, retrieved 2011 August 1
- ECMAScript on Wikipedia
- John Resig on ExmaScript 5 Strict Mode
- Douglas Crockford - Strict Mode Is Coming To Town
- IE Test Center
- IE Testing Center's Strict Mode Tests
- Install Ruby on Windows