Preliminary study on the let,const of ES6 and the __javascript of block-level scopes

Source: Internet
Author: User
Tags garbage collection

We know that the way JavaScript defines variables is VAR, but there are several problems with Var. var

The first is the scope problem,var is not for a block-level scope, but for a function scope. As an example:

function Runtowerexperiment (tower, starttime) {
  var t = starttime;

  Tower.on ("tick", function () {
    ... code that uses t ...
  });
  ... more code ...
}

This is no problem, because the variable t can be accessed in the callback function, but if we rename the variable t again in the callback function.

function Runtowerexperiment (tower, starttime) {
  var t = starttime;

  Tower.on ("tick", function () {
    ... code that uses t ...
    if (bowlingball.altitude () <= 0) {
      var t = readtachymeter ();
      ...
    }
  });
  ... more code ...
}

The latter will cover the former.

The second one is the problem of circulation .
Look at the following example:

var messages = ["meow!", "I ' m a talking cat!", "Callbacks are fun!"];

for (var i = 0; i < messages.length i++) {
    settimeout (function () {
        document.write (messages[i));
    },i*1500 );

The output result is: undefined
Because I is 3 after the For loop, I cannot access its value. Let

In order to solve these problems, ES6 put forward the LET syntax. Let can declare in {},if,for, its usage is the same as Var, but the scope is limited to block level . But isn't there a block-level scope in JavaScript? We'll talk about this in a minute. It is also important that let -defined variables do not have variable elevation . variable Elevation

Here is a simple mention of what is called a variable elevation .

var v= ' Hello World '; 
(function () { 
    alert (v); 
    var v= ' I love You '; 
} ()

The above code output results in: undefined.

Why is that so? This is because the variable elevation, the variable elevation, is to elevate the declaration of the variable to the top of the function, for example:

(function () { 
    var a= ' one '; 
    var b= ' two '; 
    var c= ' Three '; 
}) ()

is actually:

(function () { 
    var a,b,c; 
    A= ' one '; 
    B= ' two '; 
    c= ' Three '; 
} ()

So the example we have just now is actually:

var v= ' Hello World '; 
(function () { 
    var V;
    Alert (v); 
    v= ' I love You '; 
} ()

So they'll return to undefined.

This is also a problem with Var, and we don't use let to have this problem. Because it will report syntax errors :

{     
    Console.log (a);   Undefined
    Console.log (b);   referenceerror!      
    var A;
    Let B;     
}

Let's look at the block-level scope.

function Getval (boo) {
    if (boo) {
        var val = ' Red '
        //...
        Return Val
    } else {
        //Here you can access Val return
        null
    }
    //Here you can also access Val
}

And after using let:

function Getval (boo) {
    if (boo) {let
        val = ' red '
        //...
        Return Val
    } else {
        //Here is no access to Val return
        null
    }
    //Here is also no access to Val
}

Same in the For loop:

function func (arr) {for
    (var i = 0; i < arr.length; i++) {
        //I ...
    }
    Access here to get I
}

After using let:

function func (arr) {for
    (let i = 0; i < arr.length; i++) {
        //I ...
    }
    i} is not accessible here

In other words, letonly work within the curly braces . Const

Let's say const,const represents a constant index of a value.

Const AA = one;
Alert (aa)//11
AA =;
Alert (AA)//11

But the value of a constant can never change before garbage collection, so you need to use it sparingly.

Another thing to note is that the declaration of a constant must give an initial value , just like any other language. Even if we want a undefined constant, we need to declare:

Const A = undefined;
block-level scopes

Finally, mention the block-level scope that you just mentioned.

in the past, JavaScript was not a block-level scope, and we all used () to simulate block-level scopes.

(function () {
 //here is block-level scope 
}) ();

However, in ES6, {} can be a direct code block-level scope. So the content within {} cannot be accessed outside of {} .

We can look at the following code:

if (true) {
    function foo () {
        document.write ("1");
    }
}
else {
    function foo () {
        document.write ("2");
    }
}

Foo ();      2

In the JavaScript we know, the output of this code is 2. This is called a function declaration elevation, which not only promotes the function name, but also promotes the definition of the function. If you don't have a solid foundation, take a look at this article: a deep understanding of the iife of JavaScript

But in ES6, this code or throws a Referenceerroe error. because the block-level scope of {} causes the outside access to Foo (), which means that the function declaration and the LET definition variable are limited to block-level scopes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.