ECMASCRIPT5 introduces the concept of a strict pattern (Strict mode).
Its role is to not allow JavaScript to be so high-fault tolerance, let us write code for the specification requirements a little higher.
For example, when we use strict mode to write JavaScript code, we can not implicitly declare variables, must take var.
So how do you use Strict mode (Strict modes)?
When we want the code to start strict mode, we can add "use Strict" at the beginning of the code or at the beginning of the function Strict.
If we enable strict mode in the entire code (STRICT mode), then all code must follow the strict pattern specification;
If we enable in a function, then only in this function, we have to follow the strict pattern specification.
Let's write a demo and experience the experience.
<!DOCTYPE HTML> <Head> <title>Strict mode</title> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/> </Head> <Body> <Script> //Enable strict mode "Use Strict"; functiontestfunction () {varTestVar= 4; returnTestVar; } //This causes a syntax error.TestVar= 5; </Script> </Body></HTML>
In the demo above, I enabled strict mode throughout the code, but when I declare a variable outside of the function, I do not add Var and therefore do not conform to the strict pattern specification, so I will error when I run the code.
Ah, it's kind of interesting.
Just now we are in the entire code to enable strict mode, the following we will write a demo, in function to enable strict mode.
<!DOCTYPE HTML> <Head> <title>Strict mode</title> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/> </Head> <Body> <Script> functiontestfunction () {"Use Strict"; TestVar= 4; returnTestVar; } TestVar= 5; </Script> </Body></HTML>
Open the Chrome debugger:
Nani!! Why not error?!!
We look at the above code, hahaha, it turns out that we did not call TestFunction, since we did not execute it, how can it enable strict mode?
Modify the code as follows:
<!DOCTYPE HTML> <Head> <title>Strict mode</title> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/> </Head> <Body> <Script> functiontestfunction () {"Use Strict"; TestVar= 4; returnTestVar; } //Call TestFunctiontestfunction (); TestVar= 5; </Script> </Body></HTML>
And look at the chrome results:
Wow, strict mode is still very strict, if we are writing code, want to enable the strict mode, then we have to pay attention to.
Here are a few key limitations in strict mode:
Javascript |
Limit |
Example |
Variable |
Using a variable, but not using VAR to declare |
TestVar = 4; |
Delete |
Delete a variable, function, or agrument |
var testvar = 15; function TestFunc () {} Causes fault Delete TestVar; Delete TestFunc; |
Property name |
Reuse a property name when declaring an object |
Var Testobj = { Prop1:10, Prop2:15, Causes fault Prop1:20 } |
Name of parameter |
In the function parameter, reuse a parameter name |
Function TestFunc (param1,/*causes fault*/param1) { Return 1; } |
Have the potential to be a key word |
In the future it is possible to become useful keywords that cannot be used as variable names or function names |
Implements Interface Package Private Protected Public Static Yield |
Octal number |
To assign an octal number to a variable |
Var testoctal = 010; Var testescape = \010; |
This |
When this is null or undefined, it cannot be converted to a Global object (window). |
Function TestFunc () { Return this; } Var TestVar = TestFunc (); In not strict mode, the value of this testvar is a global object, but in strict mode, its value is undefined. |
Eval,arguments |
Eval,arguments cannot be used as function name or parameter name, variable name |
Var eval = 10; Var arguments = 10; |
Arguments |
In the function, we cannot change the value of the corresponding parameter by changing the arguments. |
Function Testargs (onearg) { Arguments[0] = 20; } In the non-strict mode, if we want to change the Onearg, can be changed by arguments[0], such as the above code, after execution, Onearg and arguments[0] values are 20, but in strict mode, We cannot change the value of the parameter name by arguments, arguments is just a copy. |
Arguments.callee |
Not allowed to use this |
Function (testint) { If (testint--= = 0) { Return; } Arguments.callee (Testint); } |
The Strictmode of ECMAScript5