JS strict mode
After a few months off it’s a good idea to refresh my rusty memory. I’ll write down my quick notes on the stuff I’m looking at.
Strict mode was added with ECMAScript 5 and supported on IE >= 10 and the other browsers.
You can enable it file-wide by placing this string (“directive prologue”) at the top of the file:
"use strict";
Or enable it only for a function (and placing it in the function body).
function foo()
"use strict";
...
}
What it does
Here are the most noticeable changes:
- throws an error when a variable is implicitly assigned global scope (ie when
var
isn’t used) this
is not converted to the global object when it’s null or undefined- some keywords are protected and can’t be used as variable names
with
is not allowed
The other changes are less important (to me).
Links
- here is a good reference