Introduced
ECMAscript 5 added "strict mode", which will make JavaScript run under stricter conditions, the purpose of setting up "strict mode", mainly has the following several:
- 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.
Description
This article refers to the Nanyi of the JavaScript strict mode of detailed, make a summary note. I have tested each of the examples in the original text, but there are individual test results that are different and are explained later. Those I am also more confused, if there are errors, want to point out.
Begin
Enabling strict mode is simple and requires only one string
"Use strict";
But the position of this string is not random, there are two kinds:
- Global strict mode, placed in the first line of the script file
- Local strict mode, placed in the first line of the function (recommended)
Why is it recommended to use strict mode within a function?
Because the global strict mode is not conducive to the merging of the code, when the team develops, the merge code may invalidate some other people's code.
So it's a good idea to use the local strict mode method to put the entire script file in an anonymous function that executes immediately:
(function () { "use strict"; // Some codehere});
Thinking
Why is a string enabled in strict mode? To be compatible with older browsers, a single line of strings does not affect browsers that are incompatible with strict mode.
Change
Strict mode brings a lot of grammatical changes.
A variable must be declared before assigning a value
Usually we can assign a value directly to a variable without having to declare it in advance var
, at which point the variable is a global variable. Strict mode prohibits this usage, and global variables must be explicitly declared.
"Use strict"= 2; // Error
Therefore, in strict mode, variables must be declared with var
a command before they are used.
Prohibit use with
In normal mode, we can use with
to change the scope chain, such as:
var obj = { num:1}function Test () { var num = 2 ; with (obj) { console.log (num); }} Test (); // 1
However, in strict mode, disabled with
, error:
"Use strict"; test (); // Strict mode code may not include a with statement
Create 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 eval
a 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 the variables it generates can only be used eval
internally.
"Use strict"; var x = 5; Console.log (eval (//console.log (x); // 5
Local this must be assigned a value
In normal mode, when a function compiles, this
the internal pointer is to the global Window object, but strict mode this
no longer points window
to, but undefined
. You need to assign the value yourself, what the assignment is, and what it is this
.
"Use strict"; Console.log (' window: ',this); // window function Test () { console.log (' test: ',this); // undefined }test ();
Therefore, when using the constructor, if you forget to add new,this no longer point to the global object, but instead error.
function fn () { "use strict"; this. A = 1// error
Limitations of Arguments objects
Arguments is the parameter object of a function, and strict mode restricts its use.
Assignment to arguments is not allowed
"Use strict"; var arguments = 5; // Error function Arguments () { // error //some code}
Arguments no longer track changes in parameters
// Normal Mode function Test (a) { = 5; Console.log ([a,arguments[0]])}test (2); // [5,5] // Strict mode "use strict"; function Test (a) { = 5; Console.log ([a,arguments[0]])}test (2); // [5,2]
Prohibit the use of Arguments.callee
Arguments.callee can return a function object that is being executed
// Normal Mode function Test () { console.log (Arguments.callee);} Test (); // Test () {// console.log (Arguments.callee); // }
Strict mode does not allow re-usearguments.callee
Prohibit the use of caller
In normal mode, you can use caller to return a function object that calls the current function:
function Test () { demo ();} function Demo () { console.log (demo.caller);} Test (); // Test () {// demo (); // }
Strict mode prohibits re-use of caller. So two callee and caller that look alike are no longer available in strict mode.
function must be declared at the top level
What do you mean?
We all only went to ES6. Introducing block-level scopes, 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.
// normal mode If (true ) { function FN () {console.log ( ' fn ' // fn }FN (); // FN
"Use strict"; if (true) { function fn () { console.log (' fn '); } fn (); // fn }fn (); // Error fn is not defined
Duplicate name Error object cannot have duplicate attribute
According to Nanyi's article, in strict mode, the object is not allowed to have duplicate attribute, will error. But in the actual test (Google browser), strict mode, the object property name will not be error, but like normal mode, the back of the cover before.
"Use strict"; var obj = { A:1, A:2}console.log (OBJ.A); // 2
It was later learned that the strict mode in ES6 has allowed the object to have a duplicate attribute. If you have an understanding, you can tell me next.
function cannot have a parameter with duplicate name
In normal mode, the names of the parameters are the same, followed by the preceding:
function Test (a,a,b) { console.log (a,a,b);} Test (// 2 2 3
However, in strict mode, the error will be:
"Use strict"; function Test (a,a,b) { console.log (a,a,b);} Test (+/-); // error Duplicate parameter name not allowed in this context
Disallow deletion of variables
According to Nanyi's article, the variable cannot be deleted in strict mode. Only configurable object properties that are set to True can be deleted.
However, even in normal mode, a var
declared variable cannot be deleted, whether it is a global declaration or a local declaration, but you can delete an object property:
var obj = { A:2}delete obj.a; // true //{}
In strict mode, a var
declared variable cannot be deleted, but the object's properties can also be deleted:
"Use strict"; var obj = { A:2}delete obj.a; // true //{}
Note that in normal mode, even if the variable can not be deleted, you can write delete
, no error, but in strict mode, the variables cannot be deleted delete
:
"Use strict"; var a = 2; var obj = { B:3}Delete A; // error, cannot delete, cannot use delete
Prohibit 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 num = 0100; console.log (num); // Error
But ES6 provides a new notation for octal numbers, which is preceded by a value of 0o (the first is the number 0, the second is the letter O)
"Use strict"; var num = 0o100;console.log (num);
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.
Using these words as variable names will cause an error.
"Use strict"; var let; // Error
Speaking of reserved words let
, we know that ES6 has joined let
and is const
const
not allowed as a variable name, either in normal or strict mode. Because the large browsers add their own const
reserved words, they cannot be used as variable names.
JavaScript Strict mode