Learn javascript's strict model from me

  • 2020-11-03 22:01:15
  • OfStack

1. An overview of the

In addition to normal operation mode, ECMAscript 5 adds a second operation mode: "Strict mode" (strict mode). As the name suggests, this mode allows Javascript to run under more stringent conditions.

The purpose of establishing the "strict model" is mainly as follows:

Eliminate 1 unreasonable and unscrupulous aspect of Javascript grammar and 1 odd behavior. Eliminate some unsafe areas of code operation to ensure the safety of code operation; Improve the efficiency of the compiler and increase the running speed; Set the stage for the next version of Javascript.

The "Strict Mode" represents a more logical, secure and rigorous direction for Javascript, which has been supported by major browsers, including IE 10, and has been fully embraced by many major projects.

On the other hand, the same code in "strict mode" may have different results; Statements that work in normal mode will not work in strict mode. Mastering these will help you understand Javascript in more detail and make you a better programmer.

This article describes the strict pattern in more detail.
2. Entry sign

The sign to enter strict mode is the following statement:

"use strict";

Older browsers ignored it as a 1-line plain string.

3. How to call

Strict mode has two invocation methods, suitable for different situations.

3.1 For the entire script file

Put "use strict" in line 1 of the script file, and the entire script will run in "strict mode." If this statement is not at line 1, it is invalid and the entire script runs in "normal mode." This point requires special attention if code files from different schemas are merged into one file.


<script>
 "use strict";
 console.log(" This is the strict model. ");
</script>

<script>
 console.log(" This is the normal pattern. ");
</script>

The above code indicates that there are two pieces of Javascript code in each page. The first script tag is in strict mode, the second is not.

3.2 For a single function

Put "use strict" in the first line of the function body, and the whole function runs in "strict mode."


function strict(){
 "use strict";
 return " This is the strict model. ";
}

function notStrict() {
 return " This is the normal pattern. ";
}

3.3 Workarounds for script files

Since the first method is not conducive to file merging, it is better to borrow the second method and place the entire script file in an anonymous function that executes immediately.


(function (){

 "use strict";


// some code here

})();

4. Grammar and behavior changes

Strict mode makes some changes to Javascript's syntax and behavior.

4.1 Explicit declaration of global variables

In normal mode, if a variable is not declared and assigned, the default is a global variable. Strict schema prohibits this use, and global variables must be declared explicitly.


"use strict";

v = 1; 
//  An error, v Not a statement 

for(i = 0; i < 2; i++) { 
//  An error, i Not a statement 
}

Therefore, in strict mode, variables must be declared with the var command before they are used.

4.2 Static binding

One feature of the Javascript language is that it allows for "dynamic binding", which means that certain properties and methods belong to one object, not at compile time, but at run time (runtime).

Strict mode does not allow dynamic binding, only static binding. That is, which object attributes and methods belong to must be determined at compile time. This improves compilation efficiency and makes the code easier to read with fewer surprises.

Specifically, it involves the following aspects.

(1) with statement is prohibited

Because the with statement cannot be compiled to determine which object the attribute belongs to.


"use strict";

var v = 1;

with (o){ 
//  Grammar mistakes 
 v = 2;
}

(2) Create THE SCOPE of eval

In normal mode, the Javascript language has two variable scopes (scope) : global scope and function scope. Strict mode creates a third scope: the eval scope.

In normal mode, the scope of an eval statement depends on whether it is in global scope or 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 within eval.


"use strict";
var x = 2;
console.info(eval("var x = 5; x")); 
// 5
console.info(x); 
// 2

4.3 Enhanced security measures

(1) Prohibit the this keyword from pointing to global objects


function f(){
 return !this;
}

//  return false Because the "this" Pointing to the global object, "!this" is false

function f(){
 "use strict";
 return !this;
}
//  return true Because in strict mode, this The value of undefined , so "!this" for true . 

Therefore, when using a constructor, if you forget to add new, this no longer points to the global object, but instead reports an error.


function f(){

 "use strict";

 this.a = 1;

};

f();
//  An error, this undefined 

(2) It is forbidden to traverse the call stack inside the function


function f1(){

 "use strict";

 f1.caller; 
//  An error 

 f1.arguments; 
//  An error 

}

f1();

4.4 Do not delete variables

Cannot delete variables in strict mode. Only when configurable is set to the object property of true can it be deleted.


"use strict";

var x;

delete x; 
//  Grammar mistakes 

var o = Object.create(null, 'x', {
 value: 1,
 configurable: true
});

delete o.x; 
//  Delete the success 

4.5 Explicit error reporting

In normal mode, an assignment to a read-only property of an object does not report an error, but fails silently. In strict mode, error will be reported.


function strict(){
 "use strict";
 return " This is the strict model. ";
}

function notStrict() {
 return " This is the normal pattern. ";
}

0

In strict mode, an assignment to a property read using the getter method will report an error.


function strict(){
 "use strict";
 return " This is the strict model. ";
}

function notStrict() {
 return " This is the normal pattern. ";
}

1

In strict mode, adding a new attribute to an object whose extension is forbidden will cause an error.


function strict(){
 "use strict";
 return " This is the strict model. ";
}

function notStrict() {
 return " This is the normal pattern. ";
}

2

In strict mode, deleting an unerasable attribute causes an error.


"use strict";
delete Object.prototype; 
//  An error 

4.6 Duplicate name error

Strict mode added 1 syntax error.

(1) The object cannot have properties with the same name

In normal mode, if an object has multiple properties with duplicate names, the last assigned property overrides the previous value. In strict mode, this is a syntax error.


function strict(){
 "use strict";
 return " This is the strict model. ";
}

function notStrict() {
 return " This is the normal pattern. ";
}

4

(2) The function cannot have an argument with the same name

In normal mode, if a function has multiple arguments with the same name, it can be read with arguments[i]. In strict mode, this is a syntax error.


function strict(){
 "use strict";
 return " This is the strict model. ";
}

function notStrict() {
 return " This is the normal pattern. ";
}

5

4.7 Octal notation is prohibited

In normal mode, if the first bit of an integer is 0, that means it's in base 8, like 0100 is equal to 64 in base 10. Strict mode prohibits this notation, integers whose first bit is 0 will report an error.


function strict(){
 "use strict";
 return " This is the strict model. ";
}

function notStrict() {
 return " This is the normal pattern. ";
}

6

4.8 Restrictions on arguments objects

arguments is the parameter object of the function, and its use is limited by the strict schema.

(1) arguments assignment is not allowed


function strict(){
 "use strict";
 return " This is the strict model. ";
}

function notStrict() {
 return " This is the normal pattern. ";
}

7

(2) arguments no longer tracks changes in parameters


function f(a) {
 a = 2;
 return [a, arguments[0]];
}

f(1); 
//  Normal mode is [2,2]

function f(a) {

 "use strict";
 a = 2;
 return [a, arguments[0]];

}

f(1); 
//  Strict mode is [2,1]

(3) arguments.callee is prohibited

This means that you can no longer call yourself within anonymous functions.


function strict(){
 "use strict";
 return " This is the strict model. ";
}

function notStrict() {
 return " This is the normal pattern. ";
}

9

4.9 Functions must be declared at the top level

Future versions of Javascript will introduce "block-level scope". To keep up with the new version, strict mode allows functions to be declared only at the top level of the global scope or function scope. That is, you are not allowed to declare functions in non-functional blocks of code.


"use strict";

if (true) {

 function f() { } 
//  Grammar mistakes 

}

for (var i = 0; i < 5; i++) {

 function f2() { } 
//  Grammar mistakes 

4.10 reserved words

In order to transition to future versions of Javascript, 1 reserved word has been added to strict mode: implements, interface, let, package, private, protected, static, yield.

Using these words as variable names will result in an error.


function package(protected) { 
//  Grammar mistakes 

 "use strict";

 var implements; 
//  Grammar mistakes 

}

In addition, ECMAscript 5 itself provides for another set of reserved words: class, enum, export, extends, import, super. They are also unusable.

Above is the entire content of this article, I hope to help you with your study.


Related articles: