常用的有哪些
const let
结构赋值 扩展运算符
属性名表达式
对象的方法
数组的方法
字符的方法
Set Map
Sysbol
Promise
async await
Generator
class
export import
/*
* const let
const: 能用 const 就不用 let, const 执行速度更快 语义更清晰
* 结构赋值 扩展运算符
*/
const obj = {
name: '小花',
age: 25,
sex: '男',
}
// const { name: xxx, age: age, sex: sex } = obj
// 值是 und 走默认值
const { name, age, sex, city = '哈尔滨' } = obj
// ...item 获取剩余的所有值, item 只能写到最后面
const { age: age2, ...item } = obj
const arr = [1, 2, 3]
// [0: a, 1: b, 2: c] 数组是根据下标找值
// 值是 und 走默认值
const [a, b, c, d = 4] = arr
// ...item 获取剩余的所有值
const [a1, ...item2] = arr
function fun (...item) {
// item === [1, 2, 3, 4, 5]
}
fun(1, 2, 3, 4, 5)
// string
// `` 模版字符串
// string.trim() 去前后空
// string.replaseAll()
const str1 = 'abcadef'
str1.replaceAll('a', '')
// 查找
str1.includes('abc')
// Array
// find findIndex / reduce map forEach filter
// includes
// Array.isArray(): 判断是不是数组
// fill 填充
// flat 扁平化
const arr2 = [1, 2, 3]
// 返回找到的值
// 中断循环
arr.find(dt => dt === 2)
// 返回下标
// 中断循环
arr.findIndex(dt => dt === 2)
// 找到就是 true
arr2.includes(2)
Array.isArray(arr2)
Array(100).fill(null)
const arr3 = [1, [3, [4, 5]]]
arr3.flat(Infinity)
// Object
// Object.entriens() Object.values() Object.keys() Object.defindeProper
const obj2 = {
name: '小花',
sex: '男',
age: 25,
}
// [ [name, 小花], [sex, 男], [age, 25] ]
Object.entries(obj2)
// [小花, 男, 25]
Object.values(obj2)
// [name, sex, age]
Object.keys(obj2)
// Set 去重
new Set([1, 1, 2, 2])
// Promise
new Promise((res, rej) => {
})
.then(() => {
})
.catch(() => {
})
// 只有所有的 promise 都变成已成功, 整个的 Promise.all 的状态才是已成功
// 只要有一个失败了 整个的 Promise.all 的状态是已失败
Promise.all([p1, p2, ...])
// 最先执行完的 promise 的状态 就是 Promise.race
Promise.race([p1, p2, ...])
// 立刻生成已成功的 promise
Promise.resolve(1)
// 立刻生成已失败的 promise
Promise.reject(1)
// async await
async function fun () {
// await 可以接任意值, 一般接 promise
// await 返回接口的值
// 如果 promise 异常了, 那下面的 await 不会执行了, 如果想继续往下执行
// 1. promise 后面接 catch
// 2. try catch
await promise
await promise2
}
// C -> A B
async function fun2 () {
const res = await Promise.all([A, B])
const c = await C(res)
}
// 函数
// 普通函数: this 代表当前正在执行的对象
// 箭头函数: 箭头函数没有自己的 this, 箭头函数的 this 是在定义的时候 就固定不变, 上一级作用域链找 this
// 箭头函数没有自己的 this
// 箭头函数 不能当构造函数使用 不能 new
// 箭头函数 没有 arguments
// class
class Person {
// 构造函数
constructor (user, age) {
this.user = user
this.age = age
}
method = () => {
}
// Person.x1
static x1 = 2
// Person.fun
static fun = () => {
}
}
class P extends Person {
constructor () {
// 子类没有自己的 this 必须调用 super 继承父类的 this
super()
}
}