2- new variable declaratively (var, let, const)

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 the same amount of

    --var statement ----
  4. 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='小麦xiaomai';
    console.log(a);

In the console you can print out the value of a variable var, but the question is, in the end how to understand global pollution or global variables it?

Then call the anonymous function in a variable, a call to see if you can.

var a="小麦";
window.onload= function(){
    console.log(a);
}

You can see the console output of wheat, which proves that var is indeed 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);

Then print out is how much value it? Yes, it should be 3, because var is declared globally. The second variable is var statement has been overwritten var variables are first declared.

** 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);

This time the console print out the value is 2. Scoped variables not get inside. 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);

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

Some partners will be new to JavaScript little confused, I feel let var not easy, in fact, let your data is to prevent pollution in large projects are very useful. Now look at an example of a cycle, let us look at the benefits of.

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 in the error characteristics of learning const is also very good

const a="JSPang";
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, I will not make too much of an explanation.

This lesson we learned three kinds of declarative ES6, var, let, const, these three methods have their own strengths, since it has been learning a new technology, we have to embrace it, to try the case according to your project and let it be declared with const, do not just use the var.

-----------------

So simple components look var let const three differences:

var to declare variables can be repeated statement, but let statement can not be repeated
  var block level is not limited, but is limited to block-level let
  var window will be mapped with (hang a property), but does not let mapped with window
  var can access variables in the above statement, but let there dead staging area, access variables in the above statement will complain
  const after the declaration must be assigned, otherwise it will error
  const immutable a defined amount, the error will be changed
  const and will not let the same be mapped with window, support for block-level scope, on top of being given access to the variable declaration,

 

Guess you like

Origin www.cnblogs.com/mahmud/p/11563422.html