JavaScript strict mode (use strict)

Overview

In addition to the normal running mode, ECMAscript 5 adds a second running mode: "strict mode". As the name suggests, this mode makes Javascript run under stricter conditions.

Why use strict mode

- Eliminate some unreasonable and lax aspects of Javascript syntax and reduce some weird behaviors;

- Eliminate some insecurities in code running and ensure the safety of code running;

- Improve compiler efficiency and increase running speed;

- Prepare for new versions of Javascript in the future Good foreshadowing.

"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 be run in "normal mode" will not be able to be run in "strict mode" run. Mastering these contents will help you understand Javascript in more detail and make you a better programmer.

Enter flag

"use strict";

How to call

1. For a single script

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

##2. For a single function

function strict(){

##  "use strict";

Return "This is strict mode.";

 }

Function notStrict() {

Return "This is normal mode.";

 }

## 3 Alternative ways of writing script files

(function (){

"use strict";

// some code here

})();

##Example 1:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p>浏览器按下 F12 开启调试模式,查看报错信息。</p>
<script>
"use strict";
x = 3.14;       // 报错 (x 未定义)
</script>
</body>
</html>

Example 2:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p>浏览器按下 F12 开启调试模式,查看报错信息。</p>
<script>
x = 3.14;       // 不报错 
myFunction();
function myFunction() {
   "use strict";
    y = 3.14;   // 报错 (y 未定义)
}
</script>
</body>
</html>


Changes in syntax and behavior:

1 Explicit declaration of global variables

In normal mode, if a variable is assigned a value without being declared, the default is a global variable. Strict mode prohibits this usage and global variables must be declared explicitly.

"use strict";

v = 1; // Error, v is not declared

##for (i = 0; i < 2; i++) { // Error, i is not declared

}

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


2 Static Binding

One 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 places 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.

"use strict";

var v = 1;

with (o){ // Syntax error

 v = 2;

}

(2) Create eval scope

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.

"use strict";

var x = 2;

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

console.info(x); // 2


3 Enhanced security measures

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

function f(){

Return !this;

}

// Return false because "this" points to the global object, "! this" is false

function f(){

 "use strict";

return !this;

}

// Return true, because in strict mode, the value of this is undefined, so "!this" is true.

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

function f1(){

 "use strict";

 f1.caller; // Error report

 f1.arguments; // Error report

}

f1();


4 Prohibited deletion of variables

Cannot be deleted in strict mode variable. Only object properties with configurable set to true can be deleted.

"use strict";

var x;

delete x; // Syntax error

var o = Object.create(null, {'x': {

 value: 1,

configurable: true

##}});

delete o.x; // Delete successfully


5 Explicit error reporting

In normal mode, when assigning a value to a read-only property of an object, no error will be reported, only silently fail. In strict mode, an error will be reported.

"use strict";

var o = {};

Object.defineProperty(o, "v", { value: 1, writable: false });

o.v = 2; // Error

In strict mode, use If the attribute read by the getter method is assigned a value, an error will be reported.

"use strict";

var o = {

get v () { return 1; }

};

o.v = 2; // Error reporting

Strict In this mode, adding new attributes to an object that is prohibited from expansion will result in an error.

"use strict";

var o = {};

Object.preventExtensions(o) ;

o.v = 1; // Error reporting

In strict mode, deleting an attribute that cannot be deleted will report an error.

"use strict";

delete Object.prototype; // Error


6 Duplicate name error

Strict mode has added some new syntax errors.

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

In normal mode, if the 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.

"use strict";

var o = {

 p: 1,

p: 2

##}; // 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.

"use strict";

function f(a, a, b) { // Syntax error

Return ;

}


##7 Octal notation is prohibited

In normal mode, if the first digit of the integer is 0, it means that 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.

"use strict";

var n = 0100; // Syntax error


8 Restrictions on the arguments object

arguments is the parameter object of the function, and strict mode restricts its use.

(1) Assignment of arguments is not allowed

"use strict";

arguments++; // Syntax error

var obj = { set p(arguments) { } }; // Syntax error

try { } catch (arguments) { } // Syntax error

function arguments() { } // Syntax error

var f = new Function("arguments", "'use strict'; return 17;"); // Syntax error

(2)arguments no longer tracks parameter changes

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) It is forbidden to use arguments.callee

This means that you cannot call yourself inside the anonymous function.

"use strict";

var f = function() { return arguments.callee; };

f(); // Error


9 The function must be declared at the top level

New versions of Javascript in the future Will introduce "block-level scope". 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.

"use strict";

if (true) {

 function f() { } / / Syntax error

}

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

Function f2() { } // Syntax error

}


10 Reserved words

In order to transition to new versions of Javascript in the future, strict mode has added some new reserved words: implements, interface, let, package, private, protected, public, static, yield.

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

function package(protected) { // Syntax error

 "use strict";

## var implements ; // Grammatical errors

}

Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; var public = 500; // 报错 </script> </body> </html>
submitReset Code