Strict Javascript mode and javascript Mode
Introduction
Strict mode is a way to introduce better error checking into the code. When using the strict mode, you cannot use variables that are implicitly declared, assign values to read-only properties, or add attributes to non-extensible objects.
Statement strict Mode
You can declare the strict mode by adding "use strict"; at the beginning of a file, program, or function. Such statements are called the preface to commands ". The scope of the strict mode Declaration depends on its context. If the strict mode is declared in the Global Context (outside the scope of the function), all code in the program is in the strict mode. If the strict mode is declared in the function, all code in the function is in the strict mode. For example, in the following example, all code is in strict mode, and the variable declaration outside the function may cause a syntax error "undefined variable in strict mode ".
[Javascript]View plaincopyprint?
- "Use strict ";
- Function testFunction (){
- Var testvar = 4;
- Return testvar;
- }
- // An error is reported here.
- Testvar = 5;
In the following example, only the code in testFunction is in strict mode. The variable declaration outside the function does not cause syntax errors, but the internal declaration of the function may cause syntax errors.
[Javascript]View plaincopyprint?
- Function testFunction (){
- "Use strict ";
- // An error is reported here.
- Testvar = 4;
- Return testvar;
- }
- Testvar = 5;