ES6, (ECMA2015), let, const, variable deconstruction assignment, template string

Keywords: Javascript

1. let keyword

1. role:
  * Similar to var, it is used to declare a variable
 2. characteristics:
  * Effective within block scope
  * No duplicate declaration
  * No preprocessing, no variable escalation
 3. applications:
  * Loop traversal plus listening
  * Replacing var with let is the trend
<script type="text/javascript">

    var a=10;
    let b=20; //Effective within block scope
    console.log(b)// 20
  console.log(b);// b is not defined; variables defined with let keywords in block scope are not visible globally
<script type="text/javascript">    
    let age = 12;
    let age = 13;//No duplicate declaration
  console.log(age);// age is not defined error reporting
    let age = 12;
    var sex="male"

Application: (Here) The website explains the application very well.

<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <title>01_let Keyword</title>

<button>Test 1</button>
<button>Test 2</button>
<button>Test 3</button>

<script type="text/javascript">

    let btns = document.getElementsByTagName('button');
    for(let i = 0;i<btns.length;i++){
            btns[i].onclick = function () {
                alert(i);// 0 1 2 


2. const keyword

1. role:
  * Define a constant
 2. characteristics:
  * Can not be modified
  * Other features are the same as let
 3. applications:
  * Save unchanged data
    const sex = 'male';
    sex = 'female';//Direct Save Can't Be Modified

3. Deconstruction and Assignment of Arrays and Objects

1. Understand:
  * Extracting data from objects or arrays, And assign variables(Multiple)
2. Object deconstruction assignment
  let {n, a} = {n:'tom', a:12}
3. Deconstruction assignment of arrays
  let [a,b] = [1, 'Li Bu fan'];
4. purpose
  * Assignment of multiple parameters

4. Template string

1. Template String: Simplified String Splicing
  * Template strings must be included with `
  * The changed part is defined with ${xxx}
<script type="text/javascript">
    let obj = {
        name : 'anverson',
        age : 41
    console.log('My name is:' + + ', My age is:' + obj.age);

    console.log(`My name is:${}, My age is: ${obj.age}`);





Learn a website of es6:


Posted by huszi001 on Wed, 09 Oct 2019 16:16:20 -0700