The article directories
-
- 1. An overview of the
- 2. Why strict mode
- 3. Entry sign
- 4. How to call it
-
- 4.1 Single Script
- 4.2 For single functions
- 5. Grammar and behavior changes
-
- 5.1 Explicit declaration of global Variables
- 5.2 Disallow this keyword from pointing to global objects
- 5.3 Disallow traversal of the call stack within a function
- 5.4 Do not Delete variables
- 5.5 Objects cannot have attributes with the same name
- 5.5 Functions cannot have arguments with the same name
- 5.6 An Explicit Error Message is Displayed
- 5.7 Disallow octal notation
- 5.8 Arguments Object restrictions
- 5.9 Functions must be declared at the top level
- 5.10 reserved words
- Develop reading
1. An overview of the
In addition to normal mode, ECMAscript 5 adds a second mode: strict Mode. As the name suggests, this mode makes Javascript run under more stringent conditions.
2. Why strict mode
The main objectives of the Strict Mode are as follows:
- eliminate
Javascript
Some unreasonable grammar, not rigorous place, reduce some weird behavior;- Eliminate some unsafe code operation, to ensure the safety of code operation;
- Improve the efficiency of the compiler, increase the running speed;
- For future new versions
Javascript
Lay the groundwork.
“Strict mode” represents a more reasonable, secure and rigorous development direction for Javascript, and has been supported by major browsers, including IE 10 (IE6,7,8 and 9 did not support strict mode in some tests).
On the other hand, the same code in “strict mode” may run differently; Some statements that work in normal mode will not work in strict mode.
3. Entry sign
"use strict";
Copy the code
Older browsers ignored it as a normal string.
4. How to call it
4.1 Single Script
<script>"use strict"; console.log("This is strict mode.");
</script>
Copy the code
4.2 For single functions
function strict(a){"use strict";return "This is strict mode.";
}
function notStrict(a) {return "This is the normal pattern.";
}
Copy the code
5. Grammar and behavior changes
Strict mode makes some changes to the syntax and behavior of Javascript.
5.1 Explicit declaration of global Variables
In normal mode, if a variable is assigned without a declaration, the default is global. Strict mode forbids this use, and global variables must be explicitly declared.
"use strict";
v = 1; // error, v not declared
for(i = 0; i < 2; i++) { // error, I not declared
}
Copy the code
Therefore, in strict mode, variables must be declared with the var command before they are used.
5.2 Disallow this keyword from pointing to global objects
function f(a){
// Return false because "this" refers to the global object, "! This "is false
return !this;
}
function f(a){"use strict";// Returns true, because strictly this is undefined, so "! This "to true.
return !this;
}
Copy the code
Therefore, when using the constructor, if you forget to add new, this no longer refers to the global object, but instead reports an error.
function f(a){"use strict";this.a = 1;
};
f();// Error: this is undefined
Copy the code
5.3 Disallow traversal of the call stack within a function
function f1(a){
"use strict";
f1.caller; / / an error
f1.arguments; / / an error
}
f1();
Copy the code
5.4 Do not Delete variables
Variables cannot be deleted in strict mode. Only the object property with the 64X 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; // The deletion succeeded
Copy the code
5.5 Objects cannot have attributes with the same name
In normal mode, if an object has multiple attributes with the same name, the last attribute assigned overrides the previous value. In strict mode, this is a syntax error.
"use strict";
varO = {p:1P:2
}; // Syntax error
Copy the code
5.5 Functions cannot have arguments with the same name
In normal mode, arguments[I] can be used to read functions that have multiple arguments with the same name. In strict mode, this is a syntax error.
"use strict";
function f(a, a, b) { // Syntax error
return ;
}
Copy the code
5.6 An Explicit Error Message is Displayed
In normal mode, assigning a read-only property to an object does not report an error, but silently fails. In strict mode, an error is reported.
"use strict";
var o = {};
Object.defineProperty(o, "v", { value: 1, writable: false });
o.v = 2; / / an error
Copy the code
In strict mode, an error is reported when assigning a value to a property read using a getter method.
"use strict";
varo = {get v(a) { return 1; }}; o.v =2; / / an error
Copy the code
In strict mode, an error is reported when adding a new attribute to an object for which extension is prohibited.
"use strict";
var o = {};
Object.preventExtensions(o);
o.v = 1; / / an error
Copy the code
In strict mode, an error is reported when an attribute that cannot be deleted is deleted.
"use strict";
delete Object.prototype; / / an error
Copy the code
5.7 Disallow octal notation
In normal mode, if the first digit of an integer is 0, it is octal, such as 0100 equals 64 in decimal. Strict mode disallows this representation and an error is reported if the first digit of an integer is 0.
"use strict";
var n = 0100; // Syntax error
Copy the code
5.8 Arguments Object restrictions
Arguments is the argument object to a function, whose use is restricted by strict mode.
(1) It is not allowedarguments
The assignment
"use strict";
arguments++; // Syntax error
var obj = { set p(arguments) {}};// Syntax error
try{}catch (arguments) { } // Syntax error
function arguments(a) {}// Syntax error
var f = new Function("arguments"."'use strict'; return 17;"); // Syntax error
Copy the code
(2)arguments
Parameter changes are no longer tracked
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]
Copy the code
(3) Prohibited usearguments.callee
This means that you can no longer call itself from within an anonymous function.
"use strict";
var f = function() { return arguments.callee; };
f(); / / an error
Copy the code
5.9 Functions must be declared at the top level
Future versions of Javascript will introduce “block-level scopes”. In keeping with the new version, strict mode only allows functions to be declared at the top level of a global scope or function scope. That is, you are not allowed to declare functions inside a block of code that is not a function.
"use strict";
if (true) {function f(a) {}// Syntax error
}
for (var i = 0; i < 5; i++) {function f2(a) {}// Syntax error
}
Copy the code
5.10 reserved words
In order to transition to future versions of Javascript, strict mode has added some reserved words: implements, Interface, let, Package, private, protected, public, static, and yield.
Using these words as variable names results in an error.
function package(protected) { // Syntax error
"use strict";var implements; // Syntax error
}
Copy the code
In addition, ECMAscript version 5 itself specifies that other reserved words (class, enum, export, extends, import, super), as well as const reserved words added by major browsers, cannot be used as variable names.
Develop reading
- MDN, Strict mode
- Dr. Axel Rauschmayer, JavaScript’s strict mode: A Summary
- Douglas Crockford, Strict Mode Is Coming To Town