1.JavaScript variable boost
In JavaScript, the declarations of functions and variables are promoted to the topmost part of the function.
In JavaScript, variables can be declared after they are used, that is, variables can be declared using the first.
The following two instances will get the same result:
x = 5 ; // variable x is set to 5 elem = document.getElementById ( " demo "); // find element elem.innerhtml = x; // in an element var x; // declaration x
var // declaring x 5 // variable x is set to 5 = document.getElementById ("demo"//elem.innerhtml = x; // display x in an element
To understand the above example, you need to understand "hoisting (variable elevation)".
Variable elevation: function declarations and variable declarations are always silently "lifted" by the interpreter to the topmost part of the method body.
1.1JavaScript initialization does not elevate
JavaScript only declares variables that are promoted, not initialized.
The following two instances result in a different result:
var x = 5 ; // initialize x var y = 7 ; // initialize y elem = document.getElementById ( " demo "); // find element elem.innerhtml = x + " " + y; //
var 5 // Initialize x = document.getElementById ("demo"//"" + y; // show X and y var 7 // Initialize y
The Y of instance 2 outputs the undefinedbecause the variable declaration (Var y) is promoted, but initialization (y = 7) does not promote, so the y variable is an undefined variable.
Example 2 resembles the following code:
var 5 // Initialize x var y; // declaring y = document.getElementById ("demo"// find element " " + y; // show X and y 7; // set Y to 7
1.2 Declare your variables in the head
For most programmers, JavaScript variable elevation is not known.
If the programmer does not understand the variable promotion well, the program they write is prone to some problems.
To avoid these problems, we usually declare these variables before each scope starts, which is also a normal JavaScript parsing step, which is easy for us to understand.
The strict mode does not allow the use of undeclared variables.
2.JavaScript Strict mode (use strict)
JavaScript rigorous mode (strict mode) is run under strict conditions.
2.1 Using the "Use strict" directive
The "Use strict" directive was added in JavaScript 1.8.5 (ECMASCRIPT5).
It is not a statement, but it is a literal expression that is ignored in older versions of JavaScript.
The purpose of the "use strict" is to specify that the code executes under strict conditions.
You cannot use undeclared variables in strict mode.
Ten 4 - 5.1 +.
2.2 Strict Mode Declaration
Strict mode adds "use strict" to the head of a script or function; An expression to declare.
" Use Strict " 3.14; // error (x not defined)
" Use Strict " ; myFunction (); function myFunction () { 3.14; // error (y not defined)}
Declaration inside a function is a local scope (only strict mode is used within the function):
3.14; // myFunction (); function myFunction () { "usestrict"; 3.14; // error (y not defined)}
Why use Strict mode:
eliminate some unreasonable and unreasonable JavaScript grammar, reduce some strange behavior;
- To eliminate some of the unsafe code operation, to ensure the security of code operation;
- Improve compiler efficiency, increase running speed;
- Pave the future for new versions of JavaScript.
"Strict mode" embodies JavaScript more reasonable, more secure, more rigorous development direction, including IE 10 mainstream browser, has supported it, many large projects have begun to embrace it fully.
On the other hand, the same code, in "Strict mode", may have different running results; some statements that can be run under normal mode will not work in strict mode. Mastering these elements will help you understand JavaScript more carefully and make you a better programmer.
2.3 Limitations of Strict mode
Non-declared variables are not allowed:
" Use Strict " 3.14; // error (x not defined)
The object is also a variable.
" Use Strict " = {p1:ten, p2:}; // error (x not defined)
Deleting a variable or object is not allowed.
" Use Strict " ; var 3.14 ;d elete x; // Error
Deleting functions is not allowed.
" Use Strict " ; function x (p1, p2) {}; Delete x; //
Variable names are not allowed:
" Use Strict " ; function X (P1, p1) {}; // Error
Octal is not allowed:
" Use Strict " ; var 010; // Error
Escape characters are not allowed:
" Use Strict " ; var x = \010; // Error
You are not allowed to assign a value to a read-only property:
" Use Strict " ; var obj ="x", {value:0, writable:false 3.14; // Error
A property that is read with a getter method is not allowed to be assigned a value
" Use Strict " ; var obj = {get x () {return03.14; // Error
Deleting a property that does not allow deletion is not allowed:
" Use Strict " // error
The variable name cannot use the "eval" string:
" Use Strict " ; var 3.14; // Error
Variable names cannot use the "arguments" string:
" Use Strict " ; var 3.14; // Error
The following statements are not allowed:
" Use Strict " = cos (2// error
For some security reasons, variables created at the scope eval () cannot be called:
" Use Strict " ; eval ("var x = 2"); alert (x ) ; // Error
The This keyword is forbidden to point to global objects.
function f () { return ! This// returns false because "this" points to the global object, "!this" is falsefunction f () {" use strict"; return ! This // returns True because the value of this is undefined in strict mode, so "!this" is true.
Therefore, when using the constructor, if you forget to add new,this no longer point to the global object, but instead error.
function f () { "usestrict"; This 1 ;}; f (); // error, this is not defined
2.4 Reserved Keywords
In order to transition to a new version of JavaScript in the future, strict mode has some new reserved keywords:
- Implements
- Interface
- Let
- Package
- Private
- Protected
- Public
- Static
- Yield
" Use Strict " ; var Public ; // Error
JavaScript variable Promotion