ES6
允许给函数参数赋初始值
JavaScript运行机制
事件循环
同步与异步任务
- 同步程序执行完成后,执行异步程序
单线程
- js是单线程的,一个任务完成后才能执行另一个任务
process.nextTick与setImmediate方法
事件循环
宏任务与微任务
-
宏任务:计时器、ajax、读取文件
-
微任务:promise.then
-
执行顺序
- 同步任务
- nextTick
- 微任务
- 宏任务
- setImmediate
promise 对象
-
new Promise()
let p = new Promise((resolve)>={ console.log(1); //resolve(); p.then((data)>={ console.log(data); //1 })
async函数
async function fun(){
reture 1
}
fun().then((data)>={
console.log(data);
//1
})
let p1 = new Promise((resolve)>={
resolve(1)
})
let p2 = new Promise((resolve)>={
resolve(2)
})
async function fun(){
let a = await p1;
//await 取出resolve里面的值
let b = await p2;
console.log(a);
console.log(b);
}
fun();
//1 2
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wDLfRQg5-1658153092071)(C:\Users\EBIZ\AppData\Roaming\Typora\typora-user-images\image-20210720100107310.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kVswnXnf-1658153092074)(C:\Users\EBIZ\AppData\Roaming\Typora\typora-user-images\image-20210720102745512.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KFbkIrbu-1658153092074)(C:\Users\EBIZ\AppData\Roaming\Typora\typora-user-images\image-20210720105339619.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LIIIh6f7-1658153092075)(C:\Users\EBIZ\AppData\Roaming\Typora\typora-user-images\image-20210720105924635.png)]
箭头函数不会改变它的this指向,在哪里定义函数,this指向哪里
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VTI7Qf1A-1658153092076)(C:\Users\EBIZ\AppData\Roaming\Typora\typora-user-images\image-20210720130301461.png)]
Promise对象
类与继承
原型对象实现继承
this的指向
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UPGJFIYr-1658153092078)(C:\Users\EBIZ\AppData\Roaming\Typora\typora-user-images\image-20210720145010755.png)]
call apply bind
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-K0DMoEo3-1658153092079)(C:\Users\EBIZ\AppData\Roaming\Typora\typora-user-images\image-20210720150033479.png)]
call 和apply 可以直接调用函数,只是传参的形式不一样,call是 罗列传参,而apply传参数是一个数组
而bind 不能直接调用函数,它是返回函数,bind的传参形式与call是一样的
call 的实际应用
-
call 可以实现多重继承
+function Animal() { // this指向cat this.eat = function() { console.log("吃东西"); } } function Bird() { this.fly = function() { console.log("我会飞"); } } function Cat() { // 把Animal函数的this指向改了 这样就实现了方法的继承 Animal.call(this); //核心 Bird.call(this); //核心 // this指向cat } let cat = new Cat(); cat.eat(); cat.fly();
Bird.call(this); //核心
// this指向cat
}
let cat = new Cat();
cat.eat();
cat.fly();