一、Promise 与 async
1、promise
//一//基本promise// (function(){// let p = new Promise( () => {// //同步执行// console.log(1);// })// p.then( () => {// console.log(2);// })// })//这一段代码只输出了1//二//为什么没有输出2:只有promise里面调用了resolve时,才会执行then//这段代码才会输出 1 、2// (function(){// let p = new Promise( (resolve) => {// //同步执行// console.log(1);// resolve()// })// p.then( () => {// console.log(2);// })// }())//三//当我们在调用reslove时再传入一个参数,同时then接受这参数(function(){let p = new Promise( (resolve) => {//同步执行console.log(1);resolve("hello")})p.then( (data) => {console.log(data);})}())//1// hello//就是说,then里面拿到的数据,是promise函数使用reslove传出来的结果
2、async
//一个重要的点:async函数的返回值是promise对象//例如:// async function fun(){// return 1// }// let a = fun();// console.log(a);//Promise { 1 }//如何拿到这个返回值// fun().then( (data) => {// console.log(data);//1// })//总结就是:async函数的返回值是一个promise对象,而primose的then所获取的参数就是async函数return的值//如果把上述的fun()函数写成promise对象形式// function fun(){// return new Promise( (resolve) => {// resolve(1)// })// }//五// let p1 = new Promise( (resolve) => {// resolve(1)// })// let p2 = new Promise( (resolve) => {// resolve(2)// })//如果我们要拿到p1p2的值//方式一,用then// p1.then( (data) => {// console.log(data);// })// p2.then( (data) => {// console.log(data);// })//1、2//方式二,用await// async function fun(){// let a = await p1;// let b = await p2;// console.log(a);// console.log(b);// };// fun()//题目一,输出什么// async function fun1(){// let data = await fun2();// console.log(data);// }// async function fun2(){// console.log(200);// return 100;// }// fun1();//200 100//题目二输出什么 同1 3 5 8 异2 6 7 4console.log(1);async function async1(){await async2();console.log(2);}async function async2(){console.log(3);}async1();setTimeout(function (){console.log(4);},0)new Promise(function (resolve){console.log(5);resolve()}).then( function(){console.log(6);}).then( function (){console.log(7);})console.log(8);
二、宏任务与微任务
宏任务:计时器、ajax、读取文件
微任务:promise.then
执行顺序:
- 同步程序
- process.nextTick
- 微任务
- 宏任务
-
三、闭包
```typescript //闭包:函数嵌套函数,内部函数就是闭包 // function outerFun(){ // let a = 10; // function innerFun(){ // console.log(a); // } // return innerFun; // } // let fun = outerFun(); // fun();
// //正常情况下,函数执行完成,内部变量会销毁 // function funx(){ // let a = 10; // } // console.log(a);//a is not defined,就是因为函数已经执行完成了,a变量会销毁,所以会not defined
//所以这就是闭包的一特性:内部函数没有执行完成,外部函数变量不会被销毁。
//应用:let res = (function(){let a = 10;let b = 20;function add(){return a + b;}function sub (){return a - b;}return {add,sub}})()console.log(res.add());console.log(res.sub());
<a name="Dy5Tf"></a>## 四、防抖 、节流<a name="WvtRn"></a>### 1、防抖```typescript//防抖// let inp = document.querySelector('input');//雏形原理// let t = null;// inp.oninput = function(){// if( t !== null ){// clearTimeout(t)// }// t = setTimeout( ()=>{// console.log(this.value);// },500)// }//封装防抖函数let inp = document.querySelector('input');inp.oninput = debounce(function(){console.log(this.value);},500)function debounce(fn,delay){let t = null;return function(){if( t !== null ){clearTimeout(t)}t = setTimeout( ()=>{fn.call(this)},delay)}}
2、节流
window.onscroll = throttle(()=> {console.log(111);},500)function throttle(fn ,delay){let flag = true;return function(){if(flag){setTimeout( () => {fn.call(this);flag = true},delay)}flag = false;}}
