Home>

I. Overview

In addition to normal operating mode,ecmascript 5 adds a second mode of operation:"strict mode". As the name suggests,This mode makes javascript run under stricter conditions.

The purpose of establishing "strict mode" is mainly as follows:

Eliminate some unreasonable and rigorous javascript syntax,Reduce some weird behavior; Eliminate some insecure aspects of code running,Keep the code running safely; Improve compiler efficiency,Increase running speed; Prepare for future new versions of javascript.

"Strict mode"It reflects the more reasonable, safer and more rigorous development direction of JavaScript.Major browsers including ie 10,Have already supported it,Many large projects have begun to embrace it in full.

on the other hand,Same code,In "strict mode", there may be different operating results;Some statements that can be run in "normal mode",It will not work in "strict mode".Master these things,It helps to understand javascript in more detail and make you a better programmer.

This article will introduce the "strict mode" in detail.

Entry sign

The sign to enter "strict mode" is the following line:

"use strict";

Older browsers treat it as a normal string,Ignore it.

How to call

There are two ways to call Strict Mode,Suitable for different occasions.

3.1 For the entire script file

Put "use strict" on the first line of the script file,The entire script will run in "strict mode". If this statement is not on the first line,It has no effect and the entire script runs in "normal mode". If the code files of different modes are merged into one file,This requires special attention.

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

The above code indicates thatThere are two pieces of javascript code in a web page. The previous script tag was strict mode,The latter is not.

3.2 for a single function

Put "use strict" on the first line of the function body,Then the entire function runs in "strict mode".

function strict () {
 "use strict";
 return "This is strict mode.
";
}
function notstrict () {
 return "This is normal mode.
";
}

3.3 Alternative Writing of Script Files

Because the first calling method is not conducive to file merge,So a better approach is,Borrowing the second method,Put the entire script file in an anonymous function that executes immediately.

(function () {
 "use strict";
//some code here
}) ();

Fourth, grammar and behavior changes

Strict mode for javascript syntax and behavior,Some changes have been made.

4.1 Explicit declaration of global variables

In normal mode,If a variable is assigned without a declaration,The default is a global variable.Strict mode prohibits this use,Global variables must be explicitly declared.

"use strict";
v=1;
//error, v is not declared
for (i=0;i<2;i ++) {
//error, i not declared
}

Therefore, in strict mode,Variables must first be declared with the var command,Then use it again.

4.2 Static binding

A feature of the javascript language,Is to allow "dynamic binding", that is, to which object some properties and methods belongNot determined at compile time,It is determined at runtime.

Strict mode does not allow dynamic binding,Only static binding is allowed.That is,Which object the property and method belong to,It must be determined during the compilation phase.This will help improve compilation efficiency,It also makes the code easier to read,Less accidents.

Specifically,The following aspects are involved.

(1)Prohibit the use of the with statement

Because the with statement cannot be determined at compile time,Which object the attribute belongs to.

"use strict";
var v=1;
with (o) {
//Grammatical errors
 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:the eval scope.

In normal mode,the scope of the eval statement,Depending on its global scope,Still function scope.In strict mode,The eval statement is itself a scope,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

4.3 Enhanced security measures

(1)Disallow this keyword to point to global objects

function f () {
 return! this;
}
//returns false because "this" points to the global object,"! this" is false
function f () {
 "use strict";
 return! this;
}
//returns true, because in strict mode,The value of this is undefined, so "! this" is true.

So when using the constructor,If you forget to add new, this no longer points to the global object,It's an error.

function f () {
 "use strict";
 this.a=1;
};
f ();
//error, this is undefined

(2)Disable traversing the call stack inside a function

function f1 () {
 "use strict";
 f1.caller;
//error
 f1.arguments;
//error
}
f1 ();

4.4 Forbidden to delete variables

Variables cannot be deleted in strict mode.Only object properties with configurable set to true,To be deleted.

"use strict";
var x;
delete x;
//Grammatical errors
var o=object.create (null, "x", {
 value:1, configurable:true
});
delete o.x;
//successfully deleted

4.5 Explicit errors

In normal mode,Assign a read-only property to an object,No error is reported,It just fails silently.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,Assign a property read using the getter method,Will report an error.

"use strict";
var o={
get v () {return 1;}
};
o.v=2;
//error

In strict mode,Add new attributes to objects that are not allowed to be extended,Will report an error.

"use strict";
var o=();
object.preventextensions (o);
o.v=1;
//error

In strict mode,Delete a non-removable attribute,Will report an error.

"use strict";
delete object.prototype;
//error

4.6 duplicate name error

Strict mode adds some syntax errors.

(1)Objects cannot have duplicate properties

In normal mode,If the object has multiple duplicate names,The last property assigned will overwrite the previous value.In strict mode,This is a syntax error.

"use strict";
var o={
 p:1, p:2
};
//Grammatical errors

(2)Function cannot have duplicate parameters

In normal mode,If the function has multiple arguments with the same name,Can be read with arguments [i]. In strict mode,This is a syntax error.

"use strict";
function f (a, a, b) {
//Grammatical errors
 return;
}

4.7 Forbidden octal notation

In normal mode,If the first digit of the integer is 0, it means that this is an octal number.For example, 0100 is equal to 64 in decimal. Strict mode prohibits this notation,The first digit of the integer is 0 and an error will be reported.

"use strict";
var n=0100;
//Grammatical errors

Limitations of the arguments object

arguments is the function's parameter object,Strict mode restricts its use.

(1)Assignment to arguments is not allowed

"use strict";
arguments ++;
//Grammatical errors
var obj={set p (arguments) {}};
//Grammatical errors
try {} catch (arguments) {}
//Grammatical errors
function arguments () {}
//Grammatical errors
var f=new function ("arguments", "" use strict ";return 17;");
//Grammatical errors

(2)arguments no longer track changes to 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)Prohibition of arguments.callee

this means,You cannot call yourself inside an anonymous function.

"use strict";
var f=function () {return arguments.callee;};
f ();
//error

4.9 Functions must be declared at the top level

New versions of JavaScript will introduce "block-level scoping" in the future. In order to integrate with the new version,Strict mode only allows functions to be declared at the top level of the global or function scope.That is,Functions are not allowed to be declared inside non-function code blocks.

"use strict";
if (true) {
 function f () {}
//Grammatical errors
}
for (var i=0;i<5;i ++) {
 function f2 () {}
//Grammatical errors

4.10 Reserved words

In order to transition to a new version of javascript in the future,Some reserved words have been added to strict mode:implements, interface, let, package, private, protected, public, static, yield.

Using these words as variable names will give an error.

function package (protected) {
//Grammatical errors
 "use strict";
 var implements;
//Grammatical errors
}

In addition, the fifth version of ecmascript also specifies other reserved words:class, enum, export, extends, import, super. They are also unusable.

  • Previous The usage and difference of return, return true, return false in javascript
  • Next MySQL subquery and related optimization learning tutorial