1. let 和 const 关键字
- 存在块级作用域
- 不存在变量提升
- 不能重复定义相同的变量名
- 具有暂时性死区
以前不是很理解这个暂时性死区, 最近看懂了
if条件里面定义了 tmp 但是 tmp 赋值为 “abc”
看似 tmp = ‘abc’ 是调用 var tmp = 123;实际上 let tmp; 的话。
是不允许这个变量来引用外部变量的,所有会报错
var tmp = 123;
if (true) {
tmp = 'abc'; // ReferenceError
let tmp;
}
2. 数组的解构赋值
- 等价赋值
let [a,b] = [1,2] // a = 1 b = 2
- 多余的解构 undefined
let [a,b] = [1] // a = 1 b = undefined
- 默认赋值
let [a,b = 3] = [1] // a = 1 b = 3 等价于 let [a,b = 3] = [1, undefined]
说明了只有 undefined时 默认赋值才会生效
let [a,b = 3] = [1, null] // a = 1 b = null
3. 对象的解构赋值
如果解构失败,变量的值等于undefined。
- 等价解构
let { foo, bar } = { foo: ‘aaa’, bar: ‘bbb’ };
foo // “aaa”
bar // “bbb”
- 默认解构
var {x, y = 5} = {x: 1};
x // 1
y // 5
- 对象解构也可以解构数组
let arr = [1, 2, 3];
let {0 : first, [arr.length - 1] : last} = arr;
first // 1
last // 3
对象解构还可以这样
4. 字符串的解构赋值
const [a, b, c, d, e] = ‘hello’;
a // “h”
b // “e”
c // “l”
d // “l”
e // “o”
字符串也是可以使用for of 遍历的
ES6 为字符串添加了遍历器接口
字符串新增了一些方法
String.raw
String.raw`Hi\nBB`
单纯的 `Hi\nBB` =
Hi
BB
String.raw`Hi\nBB` = Hi\nBB
String.includes() 返回布尔值,表示是否找到了参数字符串。
startsWith():返回布尔值,表示参数字符串是否在原字符串的头部。
endsWith():返回布尔值,表示参数字符串是否在原字符串的尾部。
var str = "sasa"
str.includes("a")
String.repeat() 方法返回一个新字符串,表示将原字符串重复n次。
'x'.repeat(3) // "xxx"
'hello'.repeat(2) // "hellohello"
'na'.repeat(0) // ""
String.trimStart()
String.trimEnd()
String.trim()
String.replaceAll()
5. 箭头函数
6. rest参数
7. 迭代器
8. 生成器
9. promise
Promise.then
Promise.catch
Promise.all([ ])
async
await
10. 可选链操作符
11. import (‘./index.js’) 动态导入
这个方法在vue的路由异步加载中有用到的
import(‘@/views/a’/* webpackChunkName: “recommend” */)
12.map
13. set