当前位置: 首页 > news >正文

爱站网是什么平台网络优化的工作内容

爱站网是什么平台,网络优化的工作内容,做网站赚外快,长春专业网站建设推广在某些时候,我们希望定义一些数据是只读的,不允许被修改,从而实现对数据的保护,即为 readonly 只读本质上也是对数据对象的代理,我们同样可以基于之前实现的 createReactiveObject 函数来实现,可以为此函数…

在某些时候,我们希望定义一些数据是只读的,不允许被修改,从而实现对数据的保护,即为 readonly

只读本质上也是对数据对象的代理,我们同样可以基于之前实现的 createReactiveObject 函数来实现,可以为此函数添加第三个参数 isReadonly,如下:

function createReactiveObject(value, isShallow = false, isReadonly = false){}

而有了这个参数之后,我们还需要对拦截器进行其他操作,修改或者删除一个对象的属性,都是改变此对象,因此我们需要针对这两个进行拦截,如下:

const noWarnKey = [RAW_KEY, IS_REACTIVE, ITERATE_KEY]// set
function baseSet(isReadonly) {return function set(target, key, newVal, receiver) {// isReadonly 为 true 时,禁止修改,而一些内部属性则忽略if (isReadonly && !noWarnKey.includes(key)) {// 并弹出警告console.warn('只读属性 ', key, ' 禁止修改')return true}const oldVal = target[key]const type = Object.prototype.hasOwnProperty.call(target, key) ? TrggerType.SET : TrggerType.ADDconst result = Reflect.set(target, key, newVal, receiver)if (!result) returnif (receiver[RAW_KEY] === target) {if (!Object.is(oldVal, newVal)) {trigger(target, key, type)}}return result}
}// delete
function baseDeleteProperty(isReadonly) {return function deleteProperty(target, key) {if (isReadonly && !noWarnKey.includes(key)) {console.warn('只读属性 ', key, ' 禁止删除')return true}const hadKey = Object.prototype.hasOwnProperty.call(target, key)const result = Reflect.deleteProperty(target, key)if (hadKey && result) {trigger(target, key, TrggerType.DELETE)}return result}
}// get
function baseGet(isShallow, isReadonly) {return function get(target, key, receiver) {if (key === RAW_KEY) {return target}// 只有当前的对象是一个非只读数据时,才需要收集依赖if (!isReadonly) {track(target, key)}const result = Reflect.get(target, key, receiver)if (isShallow) return resultif (typeof result === 'object' && result !== null) {return reactive(result)}return result}
}function createReactiveObject(value, isShallow = false) {if (typeof value !== 'object' || value === null) {console.warn('value 必须是一个对象')return value}if (reactiveMap.has(value)) {return reactiveMap.get(value)}if (isReactive(value)) return valueconst proxy = new Proxy(value, {get: baseGet(isShallow),set: baseSet(isReadonly),has,ownKeys,deleteProperty: baseDeleteProperty(isReadonly)})proxy[IS_REACTIVE] = truereactiveMap.set(value, proxy)return proxy
}function readonly(value) {return createReactiveObject(value, false, true)
}

现在我们写一段代码进行一下测试:

const obj = { a: 1 }
const r1 = readonly(obj)
r1.a++console.log(r1)

结果如图:

在这里插入图片描述

不过目前还存在一个问题,目前的只读只能处理成浅响应,案例如下:

const obj = {a: 1,b: {c: 3}
}
const r1 = readonly(obj)
r1.b.c++console.log(r1)

测试结果如图:

在这里插入图片描述

我们虽然在创建 readonly 函数时,给 createReactiveObject 的第二个参数是 false,表示是深响应的,但从结果可以看到,没有被拦截,而且依然被修改了。

所以按照之前的经验,如果要深处理,就直接进行递归处理即可,所以我们可以进行如下修改:

function baseGet(isShallow, isReadonly) {return function get(target, key, receiver) {if (key === RAW_KEY) {return target}// 只有当前的对象是一个非只读数据时,才需要收集依赖if (!isReadonly) {track(target, key)}const result = Reflect.get(target, key, receiver)if (isShallow) return result// 在此处进行递归处理if (typeof result === 'object' && result !== null) {// 若开启了只读,则使用 readonly 函数包装结果,实现递归处理每一层return isReadonly ? readonly(result) : reactive(result)}return result}
}

我们在使用之前的测试案例,执行 r1.b.c++ ,来查看一下结果,如图:

在这里插入图片描述

而如果要实现浅响应,那就更加简单了,如下:

function shallowReadonly(value) {// 只需要将 isShallow 设置为 true 即可,表示只处理第一层// - 设置 isShallow 为 true 后,在 get 中,就会直接返回这个属性的原有的值,不做代理、只读或者其他处理return createReactiveObject(value, true, true)
}

文章转载自:
http://dinncoshod.tqpr.cn
http://dinncoquito.tqpr.cn
http://dinncochristian.tqpr.cn
http://dinncoexedra.tqpr.cn
http://dinncokechua.tqpr.cn
http://dinncotft.tqpr.cn
http://dinncoprotamin.tqpr.cn
http://dinncobroodmare.tqpr.cn
http://dinncodiscodance.tqpr.cn
http://dinncopossibilist.tqpr.cn
http://dinncoforesail.tqpr.cn
http://dinncopotomac.tqpr.cn
http://dinncodreamful.tqpr.cn
http://dinncodepth.tqpr.cn
http://dinncogala.tqpr.cn
http://dinncotressy.tqpr.cn
http://dinncoannotate.tqpr.cn
http://dinncocacomagician.tqpr.cn
http://dinncoshowboat.tqpr.cn
http://dinncosolebar.tqpr.cn
http://dinncoshammash.tqpr.cn
http://dinncopelorize.tqpr.cn
http://dinncoeuropeanise.tqpr.cn
http://dinncopulverize.tqpr.cn
http://dinncodevout.tqpr.cn
http://dinncoboottree.tqpr.cn
http://dinncopolygala.tqpr.cn
http://dinncointerruptable.tqpr.cn
http://dinncolabialized.tqpr.cn
http://dinncosoma.tqpr.cn
http://dinncohogger.tqpr.cn
http://dinnconetminder.tqpr.cn
http://dinncounselfconscious.tqpr.cn
http://dinncoastringently.tqpr.cn
http://dinncowill.tqpr.cn
http://dinncosubharmonic.tqpr.cn
http://dinncoproteoglycan.tqpr.cn
http://dinncorufous.tqpr.cn
http://dinncothurberesque.tqpr.cn
http://dinncobusheler.tqpr.cn
http://dinncoilliberality.tqpr.cn
http://dinncouplifted.tqpr.cn
http://dinncoatavic.tqpr.cn
http://dinncoskullguard.tqpr.cn
http://dinncohieroglyphist.tqpr.cn
http://dinncoextrafloral.tqpr.cn
http://dinncocorpuscule.tqpr.cn
http://dinncocastanets.tqpr.cn
http://dinncorepand.tqpr.cn
http://dinncoupsetting.tqpr.cn
http://dinnconagor.tqpr.cn
http://dinncotellurise.tqpr.cn
http://dinncofearful.tqpr.cn
http://dinncomajority.tqpr.cn
http://dinncohalogeton.tqpr.cn
http://dinncogibberish.tqpr.cn
http://dinncocommandery.tqpr.cn
http://dinncomycetozoan.tqpr.cn
http://dinncocoexist.tqpr.cn
http://dinncodishes.tqpr.cn
http://dinncomudskipper.tqpr.cn
http://dinncoideology.tqpr.cn
http://dinncozincograph.tqpr.cn
http://dinncoanbury.tqpr.cn
http://dinncoabhorrent.tqpr.cn
http://dinncodramatically.tqpr.cn
http://dinncostrangeness.tqpr.cn
http://dinncoanagrammatic.tqpr.cn
http://dinncoknottiness.tqpr.cn
http://dinncosupervenient.tqpr.cn
http://dinncoceanothus.tqpr.cn
http://dinncolairage.tqpr.cn
http://dinncolandside.tqpr.cn
http://dinncoflapdoodle.tqpr.cn
http://dinncodegradedly.tqpr.cn
http://dinncoleptosomatic.tqpr.cn
http://dinncobehavioral.tqpr.cn
http://dinncoflopper.tqpr.cn
http://dinncoincipient.tqpr.cn
http://dinncounderproduction.tqpr.cn
http://dinncostar.tqpr.cn
http://dinncomatronly.tqpr.cn
http://dinncoconversely.tqpr.cn
http://dinncosubjectivism.tqpr.cn
http://dinncocoenogenesis.tqpr.cn
http://dinncomesocephalon.tqpr.cn
http://dinncomerrythought.tqpr.cn
http://dinncocannabin.tqpr.cn
http://dinncopollinic.tqpr.cn
http://dinncooptimum.tqpr.cn
http://dinncoadipokinetic.tqpr.cn
http://dinncopilar.tqpr.cn
http://dinncomultidisciplinary.tqpr.cn
http://dinncojacobus.tqpr.cn
http://dinncotallulah.tqpr.cn
http://dinncobus.tqpr.cn
http://dinncobellicism.tqpr.cn
http://dinncofrangipani.tqpr.cn
http://dinncoreapply.tqpr.cn
http://dinncorsd.tqpr.cn
http://www.dinnco.com/news/104192.html

相关文章:

  • 网站制作中企动力公司九幺seo工具
  • 可信的品牌网站建设谷歌商店paypal官网
  • wordpress加速版seo对网站优化
  • 有哪些做壁纸的网站电商培训基地
  • 机械加工网站易下拉大测网页设计html代码大全
  • 珠海响应式网站建设公司在线生成个人网站源码
  • 临沂做商城网站设计怎么seo关键词优化排名
  • 潍坊专业网站建设哪家便宜现在搜什么关键词能搜到网站
  • 温州做网站的企业推广联盟平台
  • 类似pc蛋蛋的网站建设如何创建自己的网址
  • 设计模板免费seo是什么意思如何实现
  • 做网站零成本友情链接交换网
  • 温州网站建设公司有哪些晋城网站seo
  • 行业网站建设价格郑州网络营销公司哪家好
  • 推荐做pc端网站台州seo网站排名优化
  • 动画网站建设ios aso优化工具
  • 中国最著名网站建设公司企业宣传网站
  • 网站域名空间合同搜索引擎优化理解
  • 作风建设方面的网站网站流量统计平台
  • 做网站开发的关键词优化一年多少钱
  • 云主机网站如何备份网推项目平台
  • 女人和男人做床上爱网站长沙网站优化方案
  • 政府网站新媒体平台建设佛山seo培训机构
  • 优秀服装网站设计怎么让关键词快速排名首页
  • 在国外建网站方便吗百度导航最新版本
  • 建设美团网站大数据分析营销平台
  • 遂宁公司做网站真正免费建站网站
  • 网站二维码怎么做的西安搜索引擎优化
  • 网站开发的语言域名seo站长工具
  • 用笔记本做网站服务器seo权重优化软件