ECMAScript5 introduces strict mode into Javascript to allow developers to select a "better" Javascript version, this version can be used in different ways to handle common and notorious errors. At the beginning, I was skeptical about the mode because only one browser (Firefox) supported the strict mode. Today, the latest versions of all mainstream browsers, including IE10 and Opera12, support strict mode. The time to use the strict mode is ripe.
What does it bring?
Strict mode brings many differences to the Javascript running mode. I divide them into two types: obvious (obvious) and subtle (subtle ). The subtle changes aim to solve the subtle problems, and I am not going to repeat them here. If you are interested in these details, please refer to the wonderful Dmitry Soshnikov article in ECMA-262-5 in Detail. Chapter 2. Strict Mode. I am more interested in introducing obvious changes: they are what you must know before you start to use strict patterns, and may bring you the most benefits.
Before introducing special features, remember that one of the goals of strict mode is to allow faster debugging of errors. The best way to help developers debug is to throw a corresponding error (throw errors when certain patterns occur) when a specific problem occurs ), instead of quietly failing or showing strange behavior (this is exactly what Javascript does today not do in strict mode ). In strict mode, the Code throws more error messages, which is a good thing, because it helps developers quickly notice problems that must be solved immediately.
Remove the with statement (Eliminates)
First, remove the with statement in strict mode. When a with statement appears in strict mode, it is considered invalid Javascript statements and throws syntax errors. Therefore, the first step to use the strict mode is to ensure that you are not using.
- // Syntax errors will occur in strict Mode
- With (location ){
- Alert (href );
- }
Prevents accidental globals)
The second point is that variables must be declared before being assigned values. In non-strict mode, a global variable with this name is automatically generated when an unspecified variable is assigned a value. This is one of the most common errors in Javascript. In strict mode, this will throw an error.
- // Throw an error in strict Mode
- (Function (){
- SomeUndeclaredVar = "foo ";
- }());
Cancel the forced conversion of this value (Eliminates this coercion)
Another important change is that when the value of this is null or undefined, it will not be forcibly converted to a global object. That is to say, this retains its original value, and may cause some code errors dependent on forced conversion. For example:
- Window. color = "red ";
- Function sayColor (){
- // In strict mode, this does not point to the window
- Alert (this. color );
- }
-
- // In either of the following situations, an error is thrown in strict mode.
- SayColor ();
- SayColor. call (null );
Basically, this value must be assigned a value, otherwise the undefined value will be retained. This means that if the new keyword is missing when the constructor is called, the following error occurs:
- Function Person (name ){
- This. name = name;
- }
-
- // Errors caused by strict Mode
- Var me = Person ("Nicolas ");
In this Code, the new keyword is missing when calling the Person constructor. The value of this is undefined. Because you cannot add attributes to undefined, this code throws an error. In non-strict mode, this is forcibly converted to a global object, so the name attribute can be correctly assigned as a global variable.
No duplicates)
When you perform a lot of encoding, you can easily define duplicate attributes in the object or define repeated parameter names for the function. In strict mode, both of these conditions will cause errors:
- // Error in strict mode-repeated Parameter
- Function doSomething (value1, value2, value1 ){
- // Code
- }
-
- // Error in strict mode-duplicate attribute
- Var object = {
- Foo: "bar ",
- Foo: "baz"
- };
Both of these are syntax errors, which will be thrown before code execution.
Safer eval () (Safer eval ())
Eval () is not removed, but it has some changes in strict mode. The biggest change is that the variables and functions declared in the eval () statement are not created in the contained domain. For example:
- (Function (){
-
- Eval ("var x = 10 ;");
-
- // In non-strict mode, x is 10
- // In strict mode, x is not declared and an error is thrown.
- Alert (x );
-
- }());
Any variable or function created by eval () remains in eval. However, you can pass the value by returning a value from eval:
- (Function (){
-
- Var result = eval ("var x = 10, y = 20; x + y ");
-
- // In both the strict and non-strict modes, the job can work normally for 30 times)
- Alert (result );
-
- }());
Errors Caused by unchangeable (Errors for immutables)
ECMAScript 5 also introduces the ability to modify attribute features, such as setting an attribute to read-only or freezing the structure of the entire object (freezing an entire object's structure ). In non-strict mode, attempts to modify an unchangeable attribute will silently fail. You may have encountered such problems when using some native APIs. Strict mode ensures that an error is thrown whenever you try to modify the attributes of an object or object in an unacceptable way.
- Var person = {};
- Object. defineProperty (person, "name "{
- Writable: false,
- Value: "Nicolas"
- });
-
- // In non-strict mode, the failure will be quietly failed. In strict mode, an error will be thrown.
- Person. name = "John ";
In this example, the name attribute is set to read-only. In non-strict mode, the name assignment will silently fail. In strict mode, an error will be thrown.
Note: If you are using the ECMAScript attribute (the ECMAScript attribute capabilities), I strongly recommend that you enable the strict mode. If you are changing the mutability of objects, you will encounter a bunch of errors, and they will be quietly taken away in non-strict mode.
How to use it?
Strict mode is easy to enable in modern browsers. You only need to add the following statement:
- "use strict";
Although this looks like a string that is not assigned a value to a variable, but it does indicate that the Javascript engine switches to the strict mode (browsers that do not support the strict mode simply read the string and continue to run as usual ). You can use it globally or in functions. Even so, you should never use it globally. Using this indicator globally means that all code in the same file is running in strict mode.
- // Do not do this
- "Use strict ";
-
- Function doSomething (){
- // This will run in strict Mode
- }
-
- Function doSomethingElse (){
- // This is also
- }
This does not seem to be a big problem, but in the world where our different scripts are stacked together (our world of aggressive script concatenation) will cause a lot of trouble. As long as a script contains this command globally, other serial scripts will also run in strict mode (which may lead to errors that you never expected ).
Therefore, it is best to use the strict mode only in the function, for example:
- Function doSomething (){
- "Use strict ";
- // Run in strict Mode
- }
-
- Function doSomethingElse (){
- // Run in non-strict Mode
- }
If you want to apply the strict mode to multiple functions, you can use the following mode (immediately-invoked function expression (IIFE )):
- (function() {
-
- "use strict";
-
- function doSomething() {
- // this runs in strict mode
- }
-
- function doSomethingElse() {
- // so does this
- }
- }());
Conclusion
I strongly recommend that everyone start using the strict mode. Now there are enough browsers to support this mode, which will save you from hiding code errors. Make sure that you do not include the enable command globally, but you can use IIFEs frequently to apply the strict mode to any number of codes. In the beginning, you will encounter an error that you have never encountered -- this is normal. After switching to the strict mode, you need to do enough tests to ensure that you have held your code. You must not just throw "use strict" into your code and assume there will be no errors. At least, you should start to use this abnormal and useful language feature to write better code.
Original: http://zhoujunmiao.com /? P = 292