1. ES6+ overview

ES6 (ECMAScript 2015) is JavaScript language 一次重 big update, 引入了许how newfeatures and 语法糖, 使 JavaScript code更加简洁, 优雅 and 可maintenance. 此 after , ECMAScript 标准每年都会release new version, 这些version被统称 for ES6+.

ES6+ 引入 主要featuresincluding:

  • let and const 声明
  • 箭头function
  • 模板string
  • Destructuring assignment
  • 默认parameter
  • 剩余parameter and scale运算符
  • object简写语法
  • Promise
  • class (Class)
  • modulesystem
  • iterators and 生成器
  • asynchronousfunction (async/await)

2. let and const 声明

ES6 引入了两个 new variable声明关键字: let and const, 用于替代 var 关键字.

2.1 let 声明

let 声明 variable具 has 块级作用域, 只 in 声明它 code块 in has 效.

// using let 声明variable let x = 10; console.log(x); // 输出: 10 // 块级作用域 if (true) { let y = 20; console.log(y); // 输出: 20 } console.log(y); // error: y is not defined // 循环in let for (let i = 0; i < 5; i++) { setTimeout(() => { console.log(i); // 输出: 0, 1, 2, 3, 4 }, 100); } // and var for 比 for (var j = 0; j < 5; j++) { setTimeout(() => { console.log(j); // 输出: 5, 5, 5, 5, 5 }, 100); }

2.2 const 声明

const 声明 variable也 is 块级作用域, 但它声明 is 常量, 一旦赋值就不能再modify.

// using const 声明常量 const PI = 3.14159; console.log(PI); // 输出: 3.14159 // 尝试modify常量会报错 PI = 3.14; // error: Assignment to constant variable. // 块级作用域 if (true) { const NAME = "张三"; console.log(NAME); // 输出: 张三 } console.log(NAME); // error: NAME is not defined // 注意: const 声明 object可以modify其property const person = { name: "张三", age: 25 }; person.age = 26; // 允许modifyproperty console.log(person); // 输出: { name: "张三", age: 26 } // 但不能重 new 赋值整个object person = { name: "李四", age: 30 }; // error: Assignment to constant variable.

3. 箭头function

箭头function is ES6 引入 一种 new function语法, 它providing了更简洁 function写法, 并且改变了 this 绑定规则.

// 传统function写法 function add(a, b) { return a + b; } // 箭头function写法 const add = (a, b) => a + b; // 单个parameter可以省略括号 const square = x => x * x; // 无parameter需要括号 const sayHello = () => console.log("Hello!"); // many 行function体需要 big 括号 and return const multiply = (a, b) => { const result = a * b; return result; }; // 箭头function and this const person = { name: "张三", age: 25, // 传统functionin this sayName: function() { setTimeout(function() { console.log(this.name); // 输出: undefined, 因 for this 指向全局object }, 100); }, // 箭头functionin this sayNameArrow: function() { setTimeout(() => { console.log(this.name); // 输出: 张三, 因 for 箭头functioninheritance out 部 this }, 100); } }; person.sayName(); person.sayNameArrow();

4. 模板string

模板string is ES6 引入 一种 new string语法, using反引号 (`) package围, 可以package含表达式 and many 行文本.

// 传统string拼接 const name = "张三"; const age = 25; const message = "我 名字 is " + name + ", 今年 " + age + " 岁. "; console.log(message); // 输出: 我 名字 is 张三, 今年 25 岁. // 模板string const message2 = `我 名字 is ${name}, 今年 ${age} 岁. `; console.log(message2); // 输出: 我 名字 is 张三, 今年 25 岁. // 模板stringin 表达式 const a = 10; const b = 20; const sumMessage = `10 + 20 = ${a + b}`; console.log(sumMessage); // 输出: 10 + 20 = 30 // many 行模板string const multiLineMessage = ` 这 is 第一行 这 is 第二行 这 is 第三行 `; console.log(multiLineMessage); // 模板stringin调用function function getGreeting() { return "Hello"; } const greetingMessage = `${getGreeting()}, ${name}!`; console.log(greetingMessage); // 输出: Hello, 张三!

5. Destructuring assignment

Destructuring assignment is ES6 引入 一种 new 语法, 它允许我们 from array or objectin提取值, 并将它们赋给variable.

5.1 array解构

// array解构 const numbers = [1, 2, 3, 4, 5]; // basic解构 const [a, b, c] = numbers; console.log(a, b, c); // 输出: 1 2 3 // 跳过某些元素 const [x, , y] = numbers; console.log(x, y); // 输出: 1 3 // 剩余parameter const [first, ...rest] = numbers; console.log(first); // 输出: 1 console.log(rest); // 输出: [2, 3, 4, 5] // 默认值 const [p, q, r, s, t, u = 6] = numbers; console.log(u); // 输出: 6 // 交换variable let num1 = 10; let num2 = 20; [num1, num2] = [num2, num1]; console.log(num1, num2); // 输出: 20 10

5.2 object解构

// object解构 const person = { name: "张三", age: 25, gender: "男", address: { city: "北京", district: "朝阳区" } }; // basic解构 const { name, age } = person; console.log(name, age); // 输出: 张三 25 // renamevariable const { name: personName, age: personAge } = person; console.log(personName, personAge); // 输出: 张三 25 // 默认值 const { gender, profession = "学生" } = person; console.log(gender, profession); // 输出: 男 学生 // 嵌套object解构 const { address: { city, district } } = person; console.log(city, district); // 输出: 北京 朝阳区 // 剩余property const { name: pName, ...otherInfo } = person; console.log(pName); // 输出: 张三 console.log(otherInfo); // 输出: { age: 25, gender: "男", address: { ... } }

6. 默认parameter

ES6 允许 for functionparameter设置默认值, 当parameter未传递 or 传递 for undefined 时, 会using默认值.

// 传统方式设置默认parameter function greet(name) { name = name || "匿名"; console.log(`Hello, ${name}!`); } // ES6 默认parameter function greet(name = "匿名") { console.log(`Hello, ${name}!`); } greet(); // 输出: Hello, 匿名! greet("张三"); // 输出: Hello, 张三! // many 个默认parameter function calculate(a, b = 10, c = 20) { return a + b + c; } console.log(calculate(5)); // 输出: 35 console.log(calculate(5, 15)); // 输出: 40 console.log(calculate(5, 15, 25)); // 输出: 45 // 默认parameter and 解构结合 function createPerson({ name = "匿名", age = 18 } = {}) { return { name, age }; } console.log(createPerson()); // 输出: { name: "匿名", age: 18 } console.log(createPerson({ name: "张三" })); // 输出: { name: "张三", age: 18 } console.log(createPerson({ name: "李四", age: 30 })); // 输出: { name: "李四", age: 30 }

7. 剩余parameter and scale运算符

7.1 剩余parameter

剩余parameter允许我们将function many 个parameter收集 to 一个arrayin.

// 剩余parameter function sum(...numbers) { return numbers.reduce((total, num) => total + num, 0); } console.log(sum(1, 2, 3)); // 输出: 6 console.log(sum(1, 2, 3, 4, 5)); // 输出: 15 // 剩余parameter and 普通parameter结合 function greet(firstName, lastName, ...hobbies) { console.log(`Hello, ${firstName} ${lastName}!`); console.log("你 爱 good is :", hobbies); } greet("张", "三", "读书", "旅行", "programming"); // 输出: // Hello, 张三! // 你 爱 good is : ["读书", "旅行", "programming"]

7.2 scale运算符

scale运算符 (...) 允许我们将array or objectunfold for many 个元素.

// scalearray const numbers1 = [1, 2, 3]; const numbers2 = [4, 5, 6]; // mergearray const mergedNumbers = [...numbers1, ...numbers2]; console.log(mergedNumbers); // 输出: [1, 2, 3, 4, 5, 6] // copyarray const copiedNumbers = [...numbers1]; console.log(copiedNumbers); // 输出: [1, 2, 3] // serving asfunctionparameter function add(a, b, c) { return a + b + c; } const nums = [1, 2, 3]; console.log(add(...nums)); // 输出: 6 // scaleobject const person = { name: "张三", age: 25 }; const extendedPerson = { ...person, gender: "男", age: 26 // 覆盖原 has property }; console.log(extendedPerson); // 输出: { name: "张三", age: 26, gender: "男" } // mergeobject const address = { city: "北京", district: "朝阳区" }; const personWithAddress = { ...person, ...address }; console.log(personWithAddress); // 输出: { name: "张三", age: 25, city: "北京", district: "朝阳区" }

8. object简写语法

ES6 providing了object字面量 简写语法, 使object creation更加简洁.

// 传统object写法 const name = "张三"; const age = 25; const person = { name: name, age: age, sayHello: function() { console.log(`Hello, ${this.name}!`); } }; // ES6 object简写语法 const person2 = { name, age, sayHello() { console.log(`Hello, ${this.name}!`); } }; console.log(person2.name); // 输出: 张三 person2.sayHello(); // 输出: Hello, 张三!