Original address: http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html
Purpose of using strict mode:
- eliminate some of the unreasonable and less rigorous JavaScript syntax, and reduce some of the bizarre behavior; - eliminate some of the unsafe code to run, keep your code safe; - increase compiler efficiency and speed; -pave the path for new versions of JavaScript in the future.
"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.
1.use strict scope A. For the entire script file
Put "use strict" in the first line of the script file, and the entire script will run in "strict mode."
B. For a single function
When use strict is placed in the first row of the function body, the entire function runs in strict mode.
When use strict is placed in the first row of the function body, the entire function runs in strict mode.
function Strict () {
"Use strict";
Return "This is strict mode. ";
}
function Notstrict () {
Return "This is normal mode. ";
}
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. Syntax and behavior change of use strict a. Global variable display declaration
In normal mode, if a variable is assigned without a declaration, the default is the global variable. Strict mode prohibits this usage, the global variable must be explicitly declared, must first be declared with the Var command, and then used.
"Use strict"// error, v not declared for// error, I not declared }
B. Static binding
One feature of the JavaScript language is that it allows "dynamic binding", that is, which object the properties and methods belong to, not determined at compile time, but at runtime.
Strict mode has some restrictions on dynamic binding. 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.
Specifically reflected in:
(1) Prohibit the use of the width statement
Because the width statement cannot determine at compile time which object the property belongs to.
"Use strict"; var v = 1; with// v = 2; }
(2) Create an eval scope (the role of Eval is to parse a string into a JS code)
In normal mode, the JavaScript language has two variable scopes (scope): global scope and function scope ( Note: There is a new block-level scope in Ecma6 ). 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 (// 5// 2
C. Enhanced security measures
- Disable the This keyword from pointing to global objects
function f () { console.log (this); // windowfunction f () { "use strict"; Console.log (this); // undefined
- Disallow traversal of the call stack inside a function
function F1 () { "use strict"// error // error } f1 ();
Error hint: uncaught TypeError: ' Caller ' and ' arguments ' are restricted function properties and cannot is accessed in this Contex T.
D. Prohibit deletion of variables
"Use strict"; var x; Delete// syntax error var o = object.create (null, {' x '1 true }}); Delete// Delete succeeded
E. Explicit error
In normal mode, the read-only property of an object is assigned, no error is made, and only silently fails. Strict mode, will be an error.
"Use strict"; var o =false// Error: uncaught typeerror:can ' t add Property V, Object is not extensible
In strict mode, an error is assigned to a property that is read using the Getter method.
"Use strict"; var o =return 1// error
F. Errors with duplicate names
- An object cannot have a property with the same name (in normal mode, if the object has more than one duplicate Name property, the last assigned value overrides the previous value.) In strict mode, this is a syntax error. )
"Use strict"; var o =12// syntax error
- A function cannot have a parameter with the same name (in normal mode, if the function has more than one parameter with the same name, it can be read with Arguments[i]. In strict mode, this is a syntax error. )
"Use strict"; function// syntax error return ; }
G. Prohibition of octal notation
In normal mode, if the first digit of the integer is 0, it indicates that this is an octal number, such as 0100 equals the decimal 64. Strict mode prohibit this notation, integer first bit is 0, will error.
"Use strict"; var// syntax error
Limitations of H.arguments objects
- Assignment to arguments is not allowed
"Use strict"; Arguments// syntax error var// syntax error trycatch// syntax error function// syntax error varnew// syntax error
- Arguments no longer track changes in parameters
function= 2; return [A, arguments[0]]; } f (// Normal mode is [2,2]function f (a) { "use strict"= 2; return [A, arguments[0]]; } f (// Strict mode = [2,1]
- Prohibit the use of Arguments.callee
"Use strict"; varfunctionreturn// error
I. function must be declared at the top level
A new version of JavaScript will introduce a "block-level scope" in the future. In order to conform to the new version, 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) { function// syntax error } for (V Ar i = 0; I < 5; i++) { function// syntax error }
J. Reserved words
In order to transition to a new version of JavaScript in the future, strict mode adds some reserved words: Implements, interface, let, package, private, protected, public, static, yield.
function // syntax error "Use strict"; var// syntax error }
In addition, the ECMAScript Fifth edition itself also stipulates other reserved words (class, enum, export, extends, import, super), as well as a const reserved word added by the major browsers themselves, and cannot be used as variable names.
JS--Use strict