Its main purpose is to do more checking.
Just add "use strict";
at the top of your code, before anything else.
For example, blah = 33;
is valid JavaScript. It means you create a completely global variable blah
.
But in strict mode it's an error because you did not use the keyword "var" to declare the variable.
Most of the time you don't mean to create global variables in the middle of some arbitrary scope, so most of the time that blah = 33
is written it is an error and the programmer didn't actually want it to be a global variable, they meant to write var blah = 33
.
It similarly disallows a lot of things that are technically valid to do. NaN = "lol"
does not produce an error. It also doesn't change the value of NaN. Using strict this (and similar weird statements) produce errors. Most people appreciate this because there is no reason to ever write NaN = "lol"
, so there was most likely a typo.
Read more at the MDN page on strict mode.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…