ES6 the let, const keyword

and let const variable declaration ES6 belong block-level scoping keywords.

First, why the need for block-level scope of it?

ES5 only global scope and function scope, not block-level scope, which bring a lot of irrational scenes.

The first scenario, the inner layer may cover the outer variable variable.

var tmp = new Date();

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

f(); // undefined

The above is the intent of the code, ifthe outer code block using the outer tmpvariable inner internal tmpvariables. However, the function fafter the execution, the result is output undefined, because the variable lift, resulting in inner tmpvariable outer cover tmpvariable.

Second scenario, the loop variable for counting the global variable leakage.

where p = '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.

Two, let

let JavaScript actually added block-level scope.

function f1() {
  let n = 5;
  if (true) {
    let n = 10;
  }
  console.log(n); // 5
}

The above function has two blocks of code, the variables are declared n, after running output 5. This means that the inner layer is not affected by the outer code block code block. If you are using twice var-defined variable n, the value of the final output is 10.

ES6 allows any nested block-level scope.

{{{{
  {let insane = 'Hello World'}
  the console.log (Insane); // error 
}}}};

The above code uses a five-level scope block, each layer is a single scope. The fourth layer scopes can not read the internal variables fifth floor scope.

Inner scope can define a variable of the same name in the enclosing scope.

{{{{
  let insane = 'Hello World';
  {let insane = 'Hello World'}
}}}};

Three, const

const Declares a read-only constant. Once declared, the value of the constant can not be changed.

const PI = 3.1415 ;
PI // 3.1415

PI = 3;
// TypeError: Assignment to constant variable.

The above code indicates that the change in value of the constant error.

Const variable declaration may not change the value, which means, once declared const variables, you must initialize the immediate future can not be left to the assignment.

const foo;
// SyntaxError: Missing initializer in const declaration

The above code indicates that, for a const, it is not only the assignment statement, the error will be.

Const same scope and let command: block-level role in the declaration occurs only within the effective

if (true) {
  const MAX = 5;
}

MAX // Uncaught ReferenceError: MAX is not defined

Constant const command statement is not the upgrade, there are also temporary dead zone, can only be used in the back position statement.

if (true) {
  console.log(MAX); // ReferenceError
  const MAX = 5;
}

The above code is called before the constant MAX statement, the result of an error.

Constant const statement, also let the same statement can not be repeated.

var message = "Hello!";
let age = 25;

// the following two lines will be given 
const the Message = "Goodbye!" ;
const age = 30;

The essence of const: const actually guaranteed, the value of the variable is not not change, but the memory address of the variable points of the stored data shall not be altered. For simple data type (numeric, string, Boolean), that value is stored in the memory address pointed to by the variable, and therefore equal to the constant. However, the composite type data (mainly objects and arrays), a variable memory address pointed to save only a pointer to the actual data, const ensure that this pointer can be fixed (i.e., always points to another fixed address) , as it points to the data structure is not a variable, it can not completely control. Therefore, an object declared as constants must be very careful.

 

Guess you like

Origin www.cnblogs.com/myitnews/p/12151526.html