Two .ES6 new declaratively

Preface:

We used only one method is to use when declaring var to declare, ES6 declaration was extended, now there are three ways a statement.

ES6 statement literally three ways:

  1. var: It is a variable shorthand, meaning can be understood as a variable.
  2. let: It is "let" means in English, it can also be understood as a declaration of meaning.
  3. const: It is also a constant in the English meaning, the ES6 is used to declare a constant, constant, you can simply understood as a constant amount.

var statement:

var is used to upgrade the ES6 in global variables, we can first make a simple example, using var to declare a variable a, then output console.log.

var a='rong';
console.log(a);  //rong

We can see rong has been printed out at the console. How to understand that its role is to declare global variables that? We use the anonymous function a parcel to him, and then call the anonymous function in a variable, a call to see if you can.

var a="rong";
window.onload= function(){
    console.log(a);  //rong
}

You can see the console output rong, which proved var truly global. If you think this is not intuitive explanation var is declared globally, you can also call the test block the way, look at the following code.

var a=2;
{
   var a=3;
}
console.log(a);  //3

Then print out is how much value it? Yes, it should be 3, because var is declared globally.

** let local declarations **

By two simple examples, we have a global var statement to have a certain understanding. It was let to corresponding with var, which is a local variable declaration. Using the example above, let us try to use the statement in the block.

var a=2;
{
   let a=3;
}
console.log(a);  //2

This time the console print out the value is 2. If we are only in the block in a statement, no external declaration, we'll get an error when printing a, display variable is not found.

{
   let a=3;
}
console.log(a);  //a is not defined

Let the two examples described above is a local variable declarations, statements let only work within the block, is not external call.

** ** cycle with Let's declaration  

for (the let I = 0; I <10; I ++) { 
  the console.log ( 'loop body:' + i); 
} 
the console.log ( 'cycle in vitro:' + i);

Will find the error console, i can not find vitro cycle of variables when you execute. By comparing the two statements, we can understand let the program on preventing pollution data is still useful. We strive to get used to a let statement, reduce pollution var statement to the global space, but also pay attention to this point in the use of vue.

** ** const constant declarations

Program development, is to change some of the variables no longer occurred after the statement in the business layer, and is simply a statement from the beginning, this variable is always the same, we need to be declared with const.

Let's use some const statement error code, learning const in error feature is also very good.

const a="rong";
var a='蓉';
console.log(a);

In the process of compiling this code, you will find that an error has been unable to compile, the reason is declared const variables we can not change. const is well understood  

  

Guess you like

Origin www.cnblogs.com/wangRong-smile/p/11510902.html