"Strict mode" is a new syntax defined by ECMA-262 Edition 5, which means that strict Javascript syntax must be used for execution. Some commonly used writing methods in the past will throw SyntaxError exceptions, such as:
1. There is no var declaration before the variable
2. Use octal syntax: var n = 023 and var s = "
1. Why use "strict mode"
The main purposes of establishing "strict mode" are as follows:
1. Eliminate some unreasonable and imprecise aspects of Javascript syntax and reduce some weird behaviors;
2. Eliminate some unsafe aspects of code running and ensure the safety of code running;
3. Improve Compiler efficiency and increased running speed;
4. Pave the way for new versions of Javascript in the future.
"Strict Mode" reflects the more reasonable, safer and more rigorous development direction of Javascript. Mainstream browsers, including IE 10, already support it, and many large projects have begun to fully embrace it.
On the other hand, the same code may have different running results in "strict mode"; some statements that can run in "normal mode" will not run in "strict mode". Mastering these contents will help you understand Javascript in more detail and make you a better programmer.
This article will introduce "strict mode" in detail.
2. Declare "strict mode"
Copy the code
The code is as follows:
"use strict";
Note: Older versions of browsers will treat it as a line of ordinary strings and ignore it.
(Strictly speaking, as long as it is not preceded by a statement that produces actual operation results, "use strict" does not need to be on the first line, such as directly following an empty semicolon.)
Copy code
The code is as follows:
Put "use strict" on the first line of the function body, and the entire function will run in "strict mode".
Copy code
The code is as follows:
function strict(){
"use strict" ;
"use strict";
// some code here
})();
4. Syntax and behavior changes in "strict mode"
"Strict mode" has made some changes to the syntax and behavior of Javascript.
1. Explicit declaration of global variables
In normal mode, when we use variables, we do not have to declare them with var first (explicit declaration), but in Strict Mode, variables must be declared with var before they can be used, otherwise it will An error occurred.
2. Static binding
A feature of the Javascript language is that it allows "dynamic binding", that is, which object certain properties and methods belong to is not determined at compile time, but at runtime.
Strict mode imposes some restrictions on dynamic binding. In some cases, only static binding is allowed. In other words, which object the properties and methods belong to is determined during the compilation stage. This will help improve compilation efficiency, make the code easier to read, and cause fewer surprises.
Specifically, it involves the following aspects.
(1) It is forbidden to use the with statement
Because the with statement cannot determine at compile time which object the attribute belongs to.
In normal mode, the Javascript language has two variable scopes: global scope and function scope. Strict mode creates a third scope: eval scope.
In normal mode, the scope of the eval statement depends on whether it is in the global scope or the function scope. In strict mode, the eval statement itself is a scope and can no longer generate global variables. The variables it generates can only be used inside eval.
3. Enhanced security measures
(1) Prohibit this keyword from pointing to the global object
"use strict";
this.a = 1;
};
f();// Error, this is undefined
(2) It is forbidden to traverse the call stack inside the function
"use strict";
f1.caller; // Error reporting
f1.arguments; // Error reporting
}
f1();
4. Deletion of variables is prohibited
Variables cannot be deleted in strict mode. Only object attributes with configurable set to true can be deleted.
5. Explicit error reporting
In normal mode, assigning a value to a read-only property of an object will not report an error, but will fail silently. In strict mode, an error will be reported.
6. Duplicate name error
Strict mode has added some new syntax errors.
(1) Objects cannot have attributes with duplicate names
In normal mode, if an object has multiple attributes with the same name, the last assigned attribute will overwrite the previous value. In strict mode, this is a syntax error.
(2) Functions cannot have parameters with duplicate names
In normal mode, if the function has multiple parameters with the same name, you can use arguments[i] to read them. In strict mode, this is a syntax error.
7. Octal notation is prohibited
In normal mode, if the first digit of an integer is 0, it means it is an octal number. For example, 0100 is equal to 64 in decimal. Strict mode prohibits this representation, the first bit of the integer is 0, and an error will be reported.
arguments is the parameter object of the function, and strict mode restricts its use.
(1) Assignment to arguments is not allowed
(2) arguments no longer track parameter changes
This means that you cannot call itself inside an anonymous function.
9. Functions must be declared at the top level
New versions of Javascript will introduce "block-level scope" in the future. In order to keep in line with the new version, strict mode only allows functions to be declared in the global scope or the top level of the function scope. That is, it is not allowed to declare functions within a non-function code block.
In order to transition to new versions of Javascript in the future, strict mode adds some new reserved words: implements, interface, let, package, private, protected, public, static, yield.
Using these words as variable names will result in an error.