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(a);//10 console.log(b);// b is not defined; variables defined with let keywords in block scope are not visible globally </script>
<script type="text/javascript"> let age = 12; let age = 13;//No duplicate declaration </script>
console.log(age);// age is not defined error reporting let age = 12; console.log(sex);//undefined var sex="male"
Application: (Here) http://es6.ruanyifeng.com/ The website explains the application very well.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>01_let Keyword</title> </head> <body> <button>Test 1</button> <br> <button>Test 2</button> <br> <button>Test 3</button> <br> <script type="text/javascript"> console.log(age); let btns = document.getElementsByTagName('button'); for(let i = 0;i<btns.length;i++){ btns[i].onclick = function () { alert(i);// 0 1 2 } } </script> </body>
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'; console.log(sex); sex = 'female';//Direct Save Can't Be Modified console.log(sex);
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:' + obj.name + ', My age is:' + obj.age); console.log(`My name is:${obj.name}, My age is: ${obj.age}`); </script>
Learn a website of es6: http://es6.ruanyifeng.com/