Zhuan
Http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html
In addition to the normal operating mode, ECMAscript 5 adds a second mode of operation: "Strict mode" (strict). As the name implies, this mode allows JavaScript to run under stricter conditions.
The purpose of setting up a "strict mode" is mainly as follows:
-Eliminate some of the unreasonable and less rigorous JavaScript syntax, and reduce some of the bizarre behavior;
-Eliminate some of the unsafe code operation, to ensure the security of code operation;
-Improve the efficiency of the compiler, increase the speed of operation;
-Pave the future for new versions of JavaScript.
"Strict mode" embodies JavaScript more reasonable, more secure, more rigorous development direction, including IE 10 mainstream browser, has supported it, many large projects have begun to embrace it fully.
On the other hand, the same code, in "Strict mode", may have different running results; some statements that can be run under normal mode will not work in strict mode. Mastering these elements will help you understand JavaScript more carefully and make you a better programmer.
This article will introduce the "strict mode" in detail.
Put "use strict" in the first line of the script file, and 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.
3.3 Workarounds for script files
Because the first method of invocation is not conducive to file merging, it is better to borrow the second method and put the entire script file in an anonymous function that executes immediately.
(function () {
"Use strict";
Some code here
})();
(2) Creating an eval scope
In normal mode, the JavaScript language has two variable scopes (SCOPE): global scope and function scope. Strict mode creates a third scope: the scope of the eval.
In normal mode, the scope of the Eval statement depends on whether it is in the global scope or at the function scope. In strict mode, the Eval statement itself is a scope that can no longer generate global variables, and it generates variables that can only be used inside the eval.
"Use strict";
var x = 2;
Console.info (eval ("var x = 5; X ")); 5
Console.info (x); 2
Javascript Strict Mode detailed