Java Reference
In-Depth Information
Strict Mode
ECMAScript 5 includes a strict mode that produces more exceptions than warnings and
prohibits the use of some deprecated features. For example, trying to assign a value to a vari-
able that is undeclared will result in an exception:
e = 2.718;
<< ReferenceError: e is not defined
Increasing the chance of errors might seem like a bad idea at first, but it's much better to spot
errors earlier on, rather than have them cause problems later. Writing code in strict mode
can also help to improve its clarity and speed, since it follows conventions and will throw
exceptions if any sloppy code practices are used.
Not using strict mode is often referred to as “sloppy mode” as it is forgiving of sloppy pro-
gramming practices. Strict mode encourages a better quality of JavaScript to be written that
befits a ninja programmer, so its use is recommended.
Strict mode simply requires the following string to be added to the first line of a JavaScript
"use strict";
This will be picked up by any JavaScript engine that uses strict mode. If the engine does not
support strict mode, this string will simply be ignored.
You can even use strict mode on a per-function basis by adding the line inside a function.
Strict mode will then only be applied to anything inside that function:
function strictly(){
"use strict";
In fact, the recommended way to invoke strict mode is to place all of your code into a self-
invoking function (covered in more detail in Chapter 12 ), like so:
Search WWH ::

Custom Search