ECMASCRIPT6 block level scope and new variable declaration (let) _javascript techniques

Source: Internet
Author: User

Many languages have block-level scopes, but JS does not, it uses VAR to declare variables, function to divide the scope, curly braces "{}" can not limit the scope of var. Variables declared with VAR have the effect of variable elevation (declaration hoisting).

ES6 adds a let, which can be declared in {}, if, for. The usage is the same as Var, but the scope is scoped to the block level, and there is no variable elevation for the variable of let declaration.

Example 1: block level scope if

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

The variable Val is declared in an if block, but can be accessed to Val outside the else block and if.

Change the Var to let, and it becomes like this.

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
}

Example 2: block-level scope for

function func (arr) {for
  (var i = 0; i < arr.length; i++) {
    //I ...
  }
  Here you can also access to I
}

The variable i is declared in the for block, but can also be accessed outside for.

To change the Var to let,for can not access I

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


Example 3: Variable elevation (first use Declaration)

function func () {
  //Val first declared after use, no error
  alert (val)//undefined
  var val;
}

Variable Val First use after declaration, output undefined, also do not error.

Change the Var to let, then the error

function func () {
  //Val first uses the declaration after the syntax error
  alert (val) let
  val;
}

Example 4: Variable elevation (declared after first judgment)

function func () {
  if (typeof val = = ' undefined ') {
    //...
  }
  var val = '
}

You can also precede the Var statement with TypeOf judgment

But to change the Var to let,if the wrong grammar

function func () {
  if (typeof val = = ' undefined ') {
    //...
  }
  Let val = ';
}

ES6 stipulates that if there is a let in the code block, this block form a closed scope from the outset. Whenever you use it before a statement, you will get an error. In a code block, the use of variables before a let declaration is not available. There is a grammatical term called "temporary Dead Zone" (temporal dead Zone), referred to as TDZ. Of course Tdz does not appear in the ES specification, it is only used to describe the image.

Let's considerations

1. Cannot repeat the statement

var and let repeat declaration
var name = ' Jack ';
Let name = ' John ';
 
Two let-repeat-let-age
=;
Let-age = 30;

Executive Times syntax error

2. With let, the anonymous function can be removed from execution

anonymous function notation
(function () {
 var jQuery = function () {};
 // ...
 window.$ = JQuery
}) ();
 
Block-level scope notation
{Let
 jQuery = function () {};
 // ...
 window.$ = JQuery;
}

The above mentioned is the entire content of this article, I hope you can enjoy.

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.