1. Let command
Tips:
- Block-level scope (valid only in the current block)
- No variable promotion (must first be declared in use)
- Let the variable monopolize the block, no longer subject to external influence
- Duplicate declarations are not allowed
In short: Let's more like the variable declaration directives of the static language we know well
ES6 has a new let command to declare variables. The usage is similar to Var, but the declared variable is valid only within the code block where the Let command resides.
Let declares that the variable has only block-level scope
‘use strict‘{ let a = 1;}console.log(a); //结果是什么?
Look at a familiar code:
var a = [];for (var i = 0; i < 10; i++) { a[i] = function () { console.log(i); };}console.log(a[6]()); //结果是什么?
If you use let, what does the following code output?
‘use strict‘var a = [];for (let i = 0; i < 10; i++) { a[i] = function () { console.log(i); };}console.log(a[6]()); // ?
At the same time, when using let, it must be declared and reused, unlike Var's variable elevation:
‘use strict‘console.log(a);let a = 1;
ES6 explicitly stipulates that if the block exists let and const, then the block will form a closed scope, usually in the declaration of apology on the use of these variables, will be error. Abbreviated as "Temporary Dead zone" (Temporal Dead Zone, abbreviated as TDZ).
See a dead zone that's not easy to spot: (Note: The code is not tested)
function bar(x=y, y=2) { return [x, y];}bar(); // 报错
The error in calling bar is because the parameter x default value equals another parameter y, and y is not declared at this time, which is a dead zone.
It is important to note that the scope of the function parameter scope and the function body are separated:
let foo = ‘outer‘;function bar(func) { let foo = ‘inner‘; console.log(func()); // outer}bar(function(){ console.log(foo);});
At the same time, let's not allow duplicate declarations
{ let a = 1; var a = 1;}{ let a = 1; let a = 2;}
2. Const command
Tips:
- Const is used to declare constants, and once declared, values cannot be changed
- Const has block-level scope
- Const cannot be variable promoted (declared after use)
- Non-repeating declaration
Const looks like a read-only object of a static language we know well
Const declares a constant, and once declared, the value will be immutable.
‘use strict‘const PI = 3.1415;PI // 3.1415PI = 3; //Error
The const directive points to the address where the variable is located, so it is possible to set the property of the variable (without changing the address of the variable), and you can use the freeze if you want to be completely immutable (including attributes).
‘use strict‘const C1 = {};C1.a = 1;console.log(C1.a); // 1 //冻结对象,此时前面用不用const都是一个效果const C2 = Object.freeze({}); C2.a = 1; //Error,对象不可扩展console.log(C2.a);
3. Global Object Properties
JavaScript, the global object is the topmost object, the browser is the Window object, node is the global object, ES5, all global variables are the properties of the global object.
In ES6, var and function declaration variables, which are attributes of global objects, let and const are not properties of global objects.
‘use strict‘let b = 2;console.log(global.b); // undefined
ES6 Starter Series One (basic)