Enter the "strict mode" flag
" Use Strict ";
Older browsers will ignore it as a single line of normal strings.
- Eliminate some unreasonable and unreasonable JavaScript grammar, reduce some strange behavior;
- To eliminate some of the unsafe code operation, to ensure the security of code operation;
- Improve compiler efficiency, increase running speed;
- Pave the future for new versions of JavaScript;
The same code, in "Strict mode", may have different results; some statements that can be run under normal mode will not work in strict mode. 1. Put "use strict" in the first line of the script file, the entire script will run in "strict mode". If the line statement is not in the first row, it is not valid and the entire script runs in normal mode. This requires special attention if the code files of different schemas are merged into one file.
<script> "usestrict"; Console.log (" this is strict mode. "); </script> <script> console.log (" this is normal mode.) "); </script>
2. Place "Use strict" in the first line of the function body, then the entire function runs in strict mode.
function Strict () { "usestrict"; return" this is strict mode. "; }function notstrict () { return' this is normal mode. "; }
The first method is not suitable for file merging, usually putting the script file into an anonymous function that executes immediately.
(function () { "usestrict"; // some codehere}) ();
Common notation for executing functions immediately
(function () {...}) () (function () {...} ())!function () {} ()
The exclamation mark is followed by the function!function and the plus sign followed by the function +function (functions () {}) (); This function is meant to tell the browser to automatically run this anonymous function because the operators of!+ () are the highest, So the functions behind them will be run first.
Syntax and behavior changes 1, in normal mode, if a variable is not declared to assign a value, the default is the global variable. Strict mode prohibits this usage, and global variables must be explicitly declared. 2, strict mode for dynamic binding custom some restrictions. In some cases, only static bindings are allowed. That is, the property and method belong to which object, which is determined at the compile stage. This facilitates compilation efficiency and makes the code easier to read and less unexpected. 3, prohibit the This keyword to the global object when using the constructor, if you forget to add new,this no longer point to the global object, but error
function f () { "usestrict"; This1; };f (); // error, this is not defined
4. Cannot delete variable in strict mode
5. Strict mode only allows functions to be declared at the top level of the global scope or function scope. That is, it is not allowed to declare a function within a block of code that is not a function.
" Use Strict " ; if (true// syntax error } for (var05; i++// syntax error }
Reference:Http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html
JavaScript Strict mode