Strict mode
ECMAscript 5 Adds a strict mode of operation, which means that JavaScript runs under more stringent conditions. Mainstream browsers, including IE 10, have supported him, and many big projects have embraced him, defining the purpose of strict patterns
1 ... eliminate some of the irrational, less rigorous, and less bizarre behavior of JavaScript syntax
2 ... eliminate some of the unsafe code from running and keep your code safe
3. Improve compilation efficiency, increase running speed
4. To pave the new version of JavaScript
Enable JavaScript Strict mode
Just add the following comment string to the JavaScript code header
<script> "use strict"; // write JavaScript code below // Note that you want to enable strict mode, "use strict " console.log ("This is strict mode"); </script>
Global mode
Put "use strict" in the first line, the entire script will run in strict mode, if not in the first row, will run in normal mode, the browser will be a string of ordinary string to ignore it
If the code files of different schemas are merged into one file, this requires special attention, strictly speaking, as long as the preceding code is not the statement that produces the actual result of the operation, "use strict" may not be in the first row
Local mode
Put "use strict" in the first row within the function, the entire function will run in strict mode.
function Strict () { "use strict" return "This is strict mode"; } function notstrict () { return "This is normal mode"; }
Module mode
Because the global schema is not conducive to file merging, it is better to borrow the local schema method to place the entire script file in an anonymous function that executes immediately
Example: If you define a module or library, you can design it in a way that is self-executing by an anonymous function:
(function() { "use strict" // write JavaScript code here });
JavaScript Strict mode