[ES6] and let const command

Reference Documents

Ruan Yifeng Great God: ECMAScript 6 Getting Started

and let const

1.let

Let only variables declared in the code block it in the effective

There is no variable lift

In a statement signed will form the use of temporary dead zone

Not allowed to repeat a statement

2. The block-level scope

ES5 no block-level scope brings a lot of irrational scenes

1. The inner cover layer variables variables

var tmp = new Date();

function f() {
  console.log(tmp);
  if (false) {
    var tmp = 'hello world';
  }
}

f(); // undefined

Where reason is that due to the variable lift

2. The loop variable for counting leakage global variables

var s = 'hello';

for (var i = 0; i < s.length; i++) {
  console.log(s[i]);
}

console.log(i); // 5

The above code, the variable iis only used to control the loop, but the loop is finished, it has not disappeared, disclosure has become a global variable.

 

to let the new block-level scope js

es6 declare expressly permitted in function block-level scope, but should be avoided. If you do need to be Ctrip function expression rather than function declaration statement

3.const

constDeclare a read-only constant. Once declared, the value of the constant can not be changed. ( Variable memory address pointed constant, simple type of data value is unchanged )

Assignment statement must, otherwise an error

constScope and letcommand the same: only block-level role in the region is declared effective, there is a temporary dead zone, can not be repeated statement

 

ES6 total of six variables declared in a way: var, function, let, const, import, class

4. The top-level object properties

In the browser environment point to the window object, the Node refers to the global object.

Among ES5, the top-level object properties and global variables are equivalent.

window.a = 1;
a // 1

ES6 in, var command and the command function declared a global variable is still the top-level variables, and let, const, class command to declare global variables do not belong to the top-level variables.

let b = 1;
window.b // undefined

5.globalThis objects

Introduced globalThisas a top target

Guess you like

Origin www.cnblogs.com/Mijiujs/p/12084160.html