ES6简介
ES6是2015年发布的一套JavaScript
标准,基本上每年都有一个新版本
1、let&const
在VSCode
中,新建一个.html
文件,输入!回车
即可快速创建出一个html模板
1、let
声明的变量有严格的局部作用域
1 2 3 4 5 6 7 8 9 10
| <script> { var a = 1; let b = 2; } console.log(a); console.log(b); </script>
|
因为b
是在代码块中使用let
定义的,所以在括号外打印会提示not defined
2、let
声明的变量只能被赋值一次
1 2 3 4 5 6 7 8
| <script> var a = 1; var a = 2; let b = 10; let b = 20; console.log(a); console.log(b); </script>
|
用var
定义的变量,可以重新赋值,用let
定义的变量不可以
3、let
不会存在变量提升
1 2 3 4 5 6
| <script> console.log(a); var a = 1; console.log(b); let b = 10; </script>
|
使用var
定义的变量没定义的时候会提升为undefined
使用let
定义的变量没定义的时候会报错
4、const
声明常量(只读变量)
1 2 3 4 5 6 7 8 9
| <script> const a = 1; a = 3; </script>
<script> const a; a = 3; </script>
|
const
声明之后不允许改变,一但声明必须初始化,否则会报错
2、解构&字符串
1、可以批量将数组中的数据赋值给多个变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <script> let arr = [1,2,3]; let a = arr[0]; let b = arr[1]; let c = arr[2]; console.log(a,b,c); </script>
<script> let arr = [1,2,3]; let[a,b,c] = arr; console.log(a,b,c); </script>
|
2、可以批量将对象中的内容赋值给多个常量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <script> const person = { name: "jack", age: 21, language: ['java', 'js', 'css'] } const name = person.name; const age = person.age; const language = person.language; console.log(name, age, language) </script>
<script> const person = { name: "jack", age: 21, language: ['java', 'js', 'css'] } const{name,age,language} = person; console.log(name, age, language) </script>
<script> const person = { name: "jack", age: 21, language: ['java', 'js', 'css'] } const { name: a, age: b, language: c } = person; console.log(a, b, c) </script>
|
3、字符串扩展
1 2 3 4 5 6 7 8 9 10 11 12
| <script> let str = "hello world"; console.log(str.startsWith("hello")); console.log(str.startsWith("hello1")); console.log(str.endsWith("world")); console.log(str.endsWith("world1")); console.log(str.includes("e")); console.log(str.includes("a")); </script>
|
4、字符串模板
1 2 3 4 5 6 7
| <script> // 可是使用``将一段字符串包含在里面 而不用像以前一样换行需要手动拼接字符串 let template = `<div> <span>hello world</span> </div>`; </script>
|
5、字符串插入变量和表达式或方法
1 2 3 4 5 6 7 8 9 10 11
| <script> const person = { name: "jack", age: 21 }; const { name, age } = person; function say() { return "你好啊"; } let str = `我叫${name},今年${age + 10}岁,${say()}`; console.log(str); </script>
|
3、函数优化
1、函数参数设置默认值
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <script> function add1(a, b) { b = b || 1; return a + b; } function add2(a, b = 1) { return a + b; } console.log(add1(10), add2(10)); </script>
|
2、传递不定参数
1 2 3 4 5 6 7 8
| <script> function getLength(...values) { console.log(values.length); } getLength(1, 2, 3); getLength(1, 2, 3, 4); </script>
|
3、箭头函数(可参考java8中的lamdba表达式)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <script> var print = obj => console.log(obj); print("hello"); </script>
<script> var sum = (a, b) => a + b; console.log(sum(10, 20)) </script>
<script> var sum = (a, b) => { let result = a + b; console.log(result) }; sum(10, 20); </script>
<script> const person = { name: "Levi", age: 19 }; var hello = ({ name }) => console.log("名字是" + name); hello(person); </script>
|
4、对象优化
1、获取对象中的keys、values以及key-value
1 2 3 4 5 6 7 8 9
| <script> const person = { name: "Levi", age: 19 }; console.log(Object.keys(person)); console.log(Object.values(person)); console.log(Object.entries(person)); </script>
|
2、将一些对象复制到目标对象
1 2 3 4 5 6 7 8 9 10
| <script> const c1 = { a: 1 }; const c2 = { b: 2 }; const c3 = { c: 3 }; Object.assign(c1, c2, c3); console.log(c1); console.log(c2); console.log(c3); </script>
|
3、声明对象简写
1 2 3 4 5 6 7 8
| <script> const name = "Levi"; const age = 19; const person = { name, age }; console.log(person.name, person.age); </script>
|
4、对象的函数属性简写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <script> let person = { name: "Levi", eat1: function (food) { console.log(this.name + "正在吃" + food); }, eat2: food => console.log(this.name + "正在吃" + food), eat3(food) { console.log(this.name + "正在吃" + food); } } person.eat1("香蕉"); person.eat2("苹果"); person.eat3("橙子"); </script>
|
5、对象扩展运算符
1、拷贝对象(深拷贝)
1 2 3 4 5 6
| <script> let p1 = { name: "Levi", age: 19 }; let p2 = { ...p1 }; console.log(p2); </script>
|
2、合并对象
1 2 3 4 5 6 7 8
| <script> let p1 = { a: 1, b: 2 }; let p2 = { b: 3, c: 4 }; let p3 = { ...p1, ...p2 }; console.log(p3); </script>
|
6、map&reduce
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <script> let arr = [1, 2, 3, 4, 5]; arr = arr.map(x => x * 2); console.log(arr); </script>
<script> let arr = [1, 2, 3, 4, 5]; let result = arr.reduce((a, b) => a + b); console.log(result); </script>
|
7、promise异步编排
新建一个json目录 其中包含如下三个json文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| { "id": 1, "name": "Levi" }
{ "id": 10, "name": "数学" }
{ "id": 10, "score": "100分" }
|
本地使用ajax依次查询出当前用户信息、用户课程信息、课程分数信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./node_modules/vue/dist/vue.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.10.0/jquery.min.js"></script> </head> <body> <script> function get(url, data) { return new Promise((resolve, reject) => { $.ajax({ url: url, data: data, success(success) { resolve(success); }, error(error) { reject(error); } }) }); }
get("json/user.json").then(data => { console.log("获取用户信息成功", data); return get(`json/user_corse_${data.id}.json`); }).then(data => { console.log("获取用户课程成功", data); return get(`json/course_score_${data.id}.json`); }).then(data => { console.log("获取课程分数成功", data); }) </script> </body> </html>
|
8、模块化
模块化就是把代码进行拆分,方便重复利用,类似Java中的导包
要使用一个包,必须先导包,在JS中模块的含义就是包
模块功能主要由两个命令构成:export
和import
export
命令用于规定模块的对外接口,可以导出一切js变量,比如:基本类型变量、函数、数组、对象
import
命令用于导入其他模块提供的功能
例如:新建hello.js
导出对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| const util = { sum(a, b) { return a + b; } }
export { util }
export const util = { sum(a, b) { return a + b; } }
|
例如:新建user.js
导出变量和函数
1 2 3 4 5 6 7
| var name = "Levi" var age = 21
function getInfo(name, age) { console.log(name, age); } export { name, age, getInfo }
|
新建一个main.js
,导入hello.js
和user.js
1 2 3 4 5 6 7
| import util from './hello'; import { name, age, getInfo } from './user';
util.sum(1, 2); console.log(name, age); getInfo("Homra", 12);
|
如果想要导入的时候自定义名字,可以在导出的时候使用default
1 2 3 4 5 6 7 8 9
| export default { sum(a, b) { return a + b; } }
import homra from './hello';
|