1.重写promise 写一个类似于promise的方法
class MyPromise{
constructor(excutor){
this.state = 'pending';
this.fulfilledEvent = [];
this.rejectEvent = [];
let resolve = (result)=>{
if(this.state !== 'pending')return
this.state = 'fulfilled';
clearTimeout(this.timer)
this.timer = setTimeout(()=>{
this.fulfilledEvent.forEach(item=>{
if(typeof item == 'function'){
item(result)
}
})
})
}
let reject = (result)=>{
if(this.state !== 'pending')return
this.state = 'rejected';
clearTimeout(this.timer)
this.timer = setTimeout(()=>{
this.rejectEvent.forEach(item=>{
if(typeof item == 'function'){
item(result)
}
})
})
}
try{
excutor(resolve,reject);
}catch(e){
reject(e)
}
};
then(resolveFn,rejectFn){
if(resolveFn === undefined){
resolveFn = () =>{}
}
if(rejectFn === undefined){
rejectFn = () =>{}
}
return new MyPromise((resolve,reject)=>{
this.fulfilledEvent.push((result)=>{
try{
let x = resolveFn(result);
x instanceof MyPromise?x.then(resolve,reject):resolve()
}catch(e){
reject(e)
}
});
this.rejectedEvent.push((result)=>{
try{
let x = rejectedFn(result);
x instanceof MyPromise? x.then(resolve,reject):resolve();
}catch(e){
reject(e);
}
});
})
}
}
let p1 = new MyPromise(function(resolve,reject){
reject()
})
p1.then(function(a){
console.log(a);
},function(){
console.log(88);
})
console.log(p1);
let p = new Promise(function(resolve,reject){
console.log(a);
})
let c = p.then(function(){
console.log(1000);
}).then(function(){
},function(){
console.log(2000);
})
2.强缓存和协商缓存
缓存 304
缓存的优点:
减少了不必要的数据传输,节省带宽
减少服务器的负担,提升网站性能
加快了客户端加载网页的速度
用户体验友好
缺点:
资源如果有更改但是客户端不及时更新会造成用户获取信息滞后,如果老版本有bug的话,情况会更加糟糕。
所以,为了避免设置缓存错误,掌握缓存的原理对于我们工作中去更加合理的配置缓存是非常重要的。
性能优化
强缓存 和协商缓存
强缓存 其实强是强制的意思。当浏览器去请求某个文件的时候,服务端就在respone header里面对该文件做了缓存配置。缓存的时间、缓存类型都由服务端控制,具体表现为:respone header 的cache-control,
常见的设置是max-age public private no-cache no-store等
cahe-control:max-age=315360000,public 这属于强缓存
每次用户正常打开这个页面,浏览器会判断缓存是否过期,没有过期就从缓存中读取数据;
缓存 本地磁盘或者浏览器缓存
强缓存总结 针对的是静态资源 比如html css 图片
cache-control: max-age=xxxx,public
客户端和代理服务器都可以缓存该资源;
客户端在xxx秒的有效期内,如果有请求该资源的需求的话就直接读取缓存,statu code:200 ,如果用户做了刷新操作,就向服务器发起http请求
cache-control: max-age=xxxx,private
只让客户端可以缓存该资源;代理服务器不缓存
客户端在xxx秒内直接读取缓存,statu code:200
cache-control: max-age=xxxx,immutable
客户端在xxx秒的有效期内,如果有请求该资源的需求的话就直接读取缓存,statu code:200 ,即使用户做了刷新操作,也不向服务器发起http请求
cache-control: no-cache
跳过设置强缓存,但是不妨碍设置协商缓存;一般如果你做了强缓存,只有在强缓存失效了才走协商缓存的,设置了no-cache就不会走强缓存了,每次请求都回询问服务端。
cache-control: no-store
不缓存,这个会让客户端、服务器都不缓存,也就没有所谓的强缓存、协商缓存了。
response header响应头 前端改不了
协商缓存
response header里设置
etag:每个文件有一个,改动文件了就变了,就是个文件hash,每个文件唯一
last-modified:文件的修改时间,精确到秒
//过程
发请求-->(强缓存)看资源是否过期-->过期-->请求服务器-->服务器对比资源是否真的过期(对比etag, last-modified:)-->没过期-->返回304状态码-->客户端用缓存的老资源。
协商缓存步骤总结:
请求资源时,把用户本地该资源的 etag 同时带到服务端,服务端和最新资源做对比。
如果资源没更改,返回304,浏览器读取本地缓存。
如果资源有更改,返回200,返回最新的资源。
3. encodeURI()编码
请求地址中如果出现非有效的Unicode编码内容,浏览器会默认进行encodeURI编码
encodeURI()
decodeURI()
encodeURI()/是不会编译/的
encodeURIComponent会编译/
encodeURIComponent
decodeURIComponent
let str = 'http://www.zhufengpeixun.cn/manage?from='+encodeURIComponent('http://127.0.0.1:5500/%E7%AC%AC%E4%B8%89%E5%A4%A9/iframe.html')
console.log(str);
let newStr = encodeURI(str)
let a = decodeURI(newStr)
console.log(a);
用于客户端页面之间处理,比如从列表页面跳转到详情页,可以把中文信息进行编译,然后再详情页进行解码
4.iframe
iframe :一个标签 可以在父页面嵌套子页面
iframe:src的地址就是嵌套子页面的地址
<!-- <iframe src="./child.html"></iframe> -->
<a href="https://www.baidu.com" target="iframeA">百度</a>
<a href="https://www.taobao.com" target="iframeA">淘宝</a>
<a href="https://www.jd.com" target="iframeA">京东</a>
<iframe src="https://www.baidu.com" name="iframeA"></iframe>