One, what is the strict mode
We usually write JavaScript code in normal mode, in addition to normal operation mode, ECMAscript 5 adds a second mode of operation: "Strict mode" (strict modes). By looking at the name, this pattern will allow JavaScript to run in more stringent environments.
Mainstream browsers, including IE 10, have supported it, and many big projects have embraced it. (GitHub Many of the above items are used in strict mode)
second, to enable strict mode
to enable strict mode for the entire script
Put a specific statement "use strict" before all statements;
Suppose you have a script reeoo.js, you can turn on strict mode like this:
"Use strict";
var name = "Reeoo";
Console.log (name);
But this writing has a natural pit, if we want to do code merging, I now want to put the Heigui.js:
and Reeoo.js to merge, originally two scripts separate execution is good, together will be an error.
uncaught Referenceerror:heigui is not defined (...)
A strict-mode script and a not-strict-mode script merge may result in a non strict pattern of script code error, the proposed code is wrapped in an immediate execution function inside.
(function () {
"use strict";
var name = "Reeoo";
}) ();
(function () {
Heigui = "db";
}) ();
This will not be an error after merging.
to enable strict mode for a function
to open a strict mode for a function, you have to put "use strict"; The declaration is done before all the statements in the function body are placed.
function Strictfun ()
{
//functional Level Strict mode syntax
' use strict ';
Console.log (' I am a strictmode function! ');
}
function Normalfun () {
console.log (' I am a mormal function! ');
}
Chrome Debug in Strict mode
I have this piece of code:
' Use strict '
name = ' Reeoo ';
Console.log (name)
Paste this code directly into the Chrome console and perform the error, but there is no error.
Obviously, it is illegal to use Var to declare variables in strict mode, but why is there no error?
What is this ghost, does chrome not support strict mode? Are you kidding...
A search on the web, the original Chrome console code is running in the eval, you can not use the Eval function strict mode (should not be completely, but what the specific chrome did, unknown), the following figure shows the Eval function to use strict mode:
To make a normal error in the strict mode in the Chrome browser, you need to set an immediate execution function in the outer layer of the Code, or any other similar measure.
(function () {
' use strict '
name = ' Reeoo ';
Console.log (name)
}) ()
That's it, okay?
Firefox code draft paper debugging Strict mode
Chrome does not want us to pack a layer of closures to run strict mode, since so troublesome, there is no other way to directly run the strict mode of code?
Firefox has a code draft paper that can run directly, shortcut keys SHIFT+F4
Exactly how strict the strict model is.
Some important limitations in strict mode
1. Variable declaration
not allowed to use a variable that is not declared
"Use strict";
Name = "Reeoo";
Error (Code grass manuscript, same below)
Exception:ReferenceError:assignment to undeclared variable name
2, modify the value of the read-only property
"Use strict";
var testobj = object.defineproperties ({}, {
prop1: {
value:10,
writable:false//A read-only property
},
prop2 : {
get:function () {
}}}
);
TESTOBJ.PROP1 = 20; Try to change the value of the Prop1
testobj.prop2 = 30;//Try to change the value of PROP2
Error in strict mode:
uncaught Typeerror:cannot Assign to read the ' Prop1 ' of #<object>
non-strict mode is the most value not to go up, and will not complain
3, modify the properties of the non-extensible
An object that behaves as if the property was set to False by adding the attribute to the extensible property.
"Use strict";
var testobj = new Object ();
Object.preventextensions (testobj); The objects processed by this method do not affect the deletion of the original object, modify it. However, new attribute members cannot be added.
Testobj.name = "Reeoo";
Strict mode error:
uncaught typeerror:can ' t add property name, object isn't extensible
non-strict mode does not error, but Testobj will not be extended.
4. Delete a variable, function, or parameter
Deletes the property set to False for the configurable attribute.
"Use strict";
var testvar = 15,testobj={};
function TestFunc () {};
Delete TestVar;
Delete TestFunc;
Object.defineproperty (Testobj, "TestVar", {
value:10,
configurable:false
});
Delete Testobj.testvar;
Error:
uncaught Syntaxerror:delete of the unqualified identifier in strict mode.
5. Define a property multiple times in the text of an object
A property is not allowed to have more than one definition in strict mode
"Use strict";
var testobj = {
prop1:10,
prop2:15,
prop1:20
};
Error (node console)
Duplicate Data Property in object literal not allowed in strict mode
Duplicate variables declared in normal mode overwrite the previous declaration and do not have an error.
Note: This problem has been fixed in the ECMASCRIPT6.
6, the strict mode does not allow formal parameter parameter name duplication
"Use strict";
function TestFunc (param1, param1) {return
1;
};
Error:
uncaught syntaxerror:duplicate parameter name not allowed in the context
7, unable to use the identifier of the future reserved word. Keep the identifier name in strict mode
The identifier (which is still not implemented in ES6) is not available in strict mode, otherwise it will be an error.
That's what happens when you use it:
uncaught syntaxerror:unexpected Strict mode reserved word
- Implements
- Interface
- Package
- Private
- Protected
- Public
- Static
- Yield
8, the strict mode does not allow the use of octal numeric parameters and escape characters
"Use strict";
var testoctal = 010;
var testescape = \010;
Error:
uncaught syntaxerror:unexpected token illegal (...)
9, when the value of this is null or undefined, the value is not converted to a global object
For example:
"Use strict";
function TestFunc () {return this
;
}
var TestVar = TestFunc ();
In non-strict mode, the value of TestVar is the Global object window, but in strict mode, the value is undefined.
10. The string "eval" cannot be used as an identifier (a variable or function name, parameter name, and so on)
"Use strict";
var eval = "hehe";
Uncaught syntaxerror:unexpected eval or arguments in strict mode
11. In strict mode, function declarations cannot be nested within statements or blocks. They can only be displayed at the top level or directly in the function body
"Use strict";
var arr = [1, 2, 3, 4, 5];
var index = null;
For (index in ARR) {
function MyFunc () {};
}
Node Console:
syntaxerror:in Strict mode code, functions can only is declared at the top level or immediately within another tion.
But this limit has been fixed in ES6.
12. Invalid eval usage in strict mode
If you declare a variable within the Eval function, you cannot use the variable outside of this function.
"Use strict";
Eval ("var TestVar = ten");
Console.log (Testvars);
Uncaught Referenceerror:testvar is not defined
13, the strict mode of "arguments" usage is invalid
The string "arguments" cannot be used as an identifier (a variable or function name, parameter name, and so on).
"Use strict";
var arguments = 1;
Uncaught syntaxerror:unexpected eval or arguments in strict mode
This is similar to the limit of the 10th above.
14, arguments within the function, cannot change the value of the member of the arguments object
"Use strict";
function Testargs (onearg) {
Arguments[0] =
In non-strict mode, you can change the value of the Onearg parameter by changing the value of the arguments[0] so that the values of Onearg and arguments[0 are all 20. In strict mode, changing the value of arguments[0] does not affect the value of Onearg because the arguments object is only a local copy.
15, not allowed to use Arguments.callee
"Use strict";
function my (testint) {
if (testint--= 0) return
;
Arguments.callee (testint--);
}
my (100);
That's what happens when you use it:
uncaught typeerror: ' Caller ', ' callee ', and ' arguments ' properties may is accessed on strict mode functions O R The arguments objects for calls to them
16. Not allowed with
"Use strict";
With (Math) {
x = cos (3);
y = tan (7);
}
Uncaught syntaxerror:strict mode code may don't include a with statement
Why to use strict mode
since there are so many restrictions on this model, why should I use it? Does the idle egg hurt? Of course 8 is,
JavaScript as a first-time scripting language for browsers, fault tolerance is very good, even if sometimes your code is not standard, it will not report errors, but this will sometimes become a code hidden trouble. After the rigorous mode is turned on, some of the irrational and imprecise syntax of JavaScript is controlled, allowing you to write JavaScript code more rigorously and become a better programmer. Strict mode is the product of the ES5 era, ES2015 already on the road of popularization, it is time to use strict mode!
Reference
- Strict mode
- Strict mode
Original: http://reeoo.me/archives/strictmode.html