1 简单分类
1 function hh(){}
2 var hh = function (){}
3 (function() {})()
4 () => {}
5 {hh() {}}
2 基础使用
1 参数问题
1 function test(a=1, b=2, c=3) {}
2 function test( {a, b, c} = {}) {}
3 function hh({a, b, c}) { console.log(a, b, c) }
hh({a: 1, b: 2, c: 3})
4 function hh( [...arr]) { console.log(arr)}
hh([1, 2, 3])
5 function hh(...arr) {console.log(arr)}
hh(1, 2, 3)
2 箭头函数
1 msg => {}
2 msg => console(msg)
3 生成器函数
1 🔶 简单描述
1 function* 这种声明方式, 会定义一个生成器函数, 它返回一个 Generator 对象
2 调用生成器函数, 并不会马上执行它里面的语句, 而是返回一个这个生成器的 迭代器(iterator) 对象
3 当这个迭代器的 next() 方法被首次 (后续) 调用时, 其内的语句会执行到第一个 (后续) 出现 yield 的位置为止
2 🔶 代码范式
function *test() {
yield console.log(1)
yield console.log(2)
}
let obj = test()
obj.next()
obj.next()
3 还有很多很多没弄, 暂时没时间弄了, 用的地方也比较少
4 异步函数 async
1 简单描述:
1 async 和 await 关键字, 让我们可以更简洁的写出基于 Promise 的异步行为, 从而无需刻意地链式调用 promise
2 async 用来声明函数是异步的
3 await 用于等待一个 async 函数的返回值
2 代码范式
5 回调函数
6 构造函数
6 数据类型 - 相关方法
1 数字 -> 字符串
1 var hh = String(num)
2 var hh = num.toFixed(xx)
3 num.toString(xx)
4 常num + ""
2 字符串 -> 数字
1 var hh = Number(str)
2 var hh = parseInt(str)
3 var hh = parseFloate(str)
3 其他类型 -> 布尔值
1 Boolean(123)
2 (!!123)
4 其它
1 isNaN(v)
2 typeof x
3 console.log( Object.prototype.toString.call( hh ) )
4 arr instanceof Object
7 常用 API 方法
1 变量复制方法
1 浅复制
let a = b
2 深复制
let date = [1, 2, 3]
let _date = JSON.parse(JSON.stringify(date))
let arr = [1, 2, 3]
let [...hh] = arr
let obj = {a: 1, b: 2}
let {...kk} = obj
2 JSON 方法
1 JSON.parse(jsonStr)
2 JSON.stringify(obj)
3 改变 this 指向
1 bind()
1 function test() {console.log(this)} -> test = test.bind(obj)
2 var hh = function() {}.bind(obj)
3 setInterval(function() {}}.bind(obj),1000)
2 call()
1 function test(a, b) {console.log(this)} -> test = test.call(obj, 1, 2)
2 var hh = function() {}.call(obj, 1, 2)
3 apply()
1 function test(a, b) {console.log(this)} -> test = test.apply(obj, [5, 6])