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

商业网站建设开发中心seo从零开始到精通200讲解

商业网站建设开发中心,seo从零开始到精通200讲解,南京做南京华美整容网站,临沂市住房和城乡建设厅网站1. Proxy 1. 监听对象的变化 有一个对象,我们希望监听这个对象中的属性被设置或获取的过程 我们可以通过 Object.defineProperty 来实现 const obj {name: "why",age: 18,height: 1.88 }// 需求: 监听对象属性的所有操作 // 监听属性的操作 // 1.针对…

1. Proxy

1. 监听对象的变化

有一个对象,我们希望监听这个对象中的属性被设置或获取的过程

我们可以通过 Object.defineProperty 来实现

const obj = {name: "why",age: 18,height: 1.88
}// 需求: 监听对象属性的所有操作
// 监听属性的操作
// 1.针对一个属性
// let _name = obj.name
// Object.defineProperty(obj, "name", {
//   set: function(newValue) {
//     console.log("监听: 给name设置了新的值:", newValue)
//     _name = newValue
//   },
//   get: function() {
//     console.log("监听: 获取name的值")
//     return _name
//   }
// })// 2.监听所有的属性: 遍历所有的属性, 对每一个属性使用defineProperty
Object.keys(obj).forEach(key => {let value = obj[key]Object.defineProperty(obj, key, {set: function (newValue) {console.log(`监听: 给${key}设置了新的值:`, newValue)value = newValue},get: function () {console.log(`监听: 获取${key}的值`)return value}})
})// console.log(obj.name)
// obj.name = "kobe"
console.log(obj.age)
obj.age = 17
console.log(obj.age)

2. Object.defineProperty 实现的缺点

1.首先,Object.defineProperty设计的初衷,不是为了去监听截止一个对象中所有的属性的

2.其次,如果我们想监听更加丰富的操作,比如新增属性、删除属性,那么Object.defineProperty是无能为力的

所以我们需要使用到 Proxy 进行代理

3. Proxy代理的使用

在ES6中,新增了一个Proxy类,这个类从名字就可以看出来,是用于帮助我们创建一个代理的,也就是说,如果我们希望监听一个对象的相关操作,那么我们可以先创建一个代理对象(Proxy对象),之后对该对象的所有操作,都通过代理对象来完成,代理对象可以监听我们想要对原对象进行哪些操作

Proxy代理需要我们首先 new Proxy对象,并且传入需要侦听的对象以及一个处理对象,可以称之为handler

const p = new Proxy(target, handler)

其次,我们之后的操作都是直接对Proxy的操作,而不是原有的对象,因为我们需要在handler里面进行侦听

如果我们想要侦听某些具体的操作,那么就可以在handler中添加对应的捕捉器(Trap)

image.png

const obj = {name: "why",age: 18,height: 1.88
}// 1.创建一个Proxy对象
const objProxy = new Proxy(obj, {set: function(target, key, newValue) {console.log(`监听: 监听${key}的设置值: `, newValue)target[key] = newValue},get: function(target, key) {console.log(`监听: 监听${key}的获取`)return target[key]}
})// 2.对obj的所有操作, 应该去操作objProxy
// console.log(objProxy.name)
// objProxy.name = "kobe"
// console.log(objProxy.name)
// objProxy.name = "james"objProxy.address = "广州市"
console.log(objProxy.address)

4. hander的监听捕获器

image.png

对常规对象属性的监听

const obj = {name: "why",age: 18,height: 1.88
}// 1.创建一个Proxy对象
const objProxy = new Proxy(obj, {set: function(target, key, newValue) {console.log(`监听: 监听${key}的设置值: `, newValue)target[key] = newValue},get: function(target, key) {console.log(`监听: 监听${key}的获取`)return target[key]},deleteProperty: function(target, key) {console.log(`监听: 监听删除${key}属性`)delete obj.name},has: function(target, key) {console.log(`监听: 监听in判断 ${key}属性`)return key in target}
})delete objProxy.nameconsole.log("age" in objProxy)

对函数对象的监听

function foo(num1, num2) {console.log(this, num1, num2)
}const fooProxy = new Proxy(foo, {apply: function(target, thisArg, otherArgs) {console.log("监听执行了apply操作")target.apply(thisArg, otherArgs)},construct: function(target, otherArray) {console.log("监听执行了new操作")console.log(target, otherArray)return new target(...otherArray)}
})// fooProxy.apply("abc", [111, 222])
new fooProxy("aaa", "bbb")

2. Reflect

1. Reflect作用

Reflect也是ES6新增的一个API,它是一个对象,字面的意思是反射,它主要提供了很多操作JavaScript对象的方法,有点像Object中操作对象的方法

在早期的ECMA规范中没有考虑到这种对 对象本身 的操作如何设计会更加规范,所以将这些API放到了Object上面,但是Object作为一个构造函数,这些操作实际上放到它身上并不合适,另外还包含一些类似于 in、delete操作符,让JS看起来是会有一些奇怪的,所以在ES6中新增了Reflect,让我们这些操作都集中到了Reflect对象上

2. Reflect与OBject的区别

删除对象属性的操作

"use strict"const obj = {name: "why",age: 18
}Object.defineProperty(obj, "name", {configurable: false
})
// Reflect.defineProperty()// 1.用以前的方式进行操作
// delete obj.name
// if (obj.name) {
//   console.log("name没有删除成功")
// } else {
//   console.log("name删除成功")
// }// 2.Reflect
if (Reflect.deleteProperty(obj, "name")) {console.log("name删除成功")
} else {console.log("name没有删除成功")
}

3. Reflect常见方法

image.png

3. Proxy与Reflect共同完成代理

const obj = {name: "why",age: 18
}const objProxy = new Proxy(obj, {set: function(target, key, newValue, receiver) {// target[key] = newValue// 1.好处一: 代理对象的目的: 不再直接操作原对象// 2.好处二: Reflect.set方法有返回Boolean值, 可以判断本次操作是否成功const isSuccess = Reflect.set(target, key, newValue)if (!isSuccess) {throw new Error(`set ${key} failure`)}},get: function(target, key, receiver) {}
})// 操作代理对象
objProxy.name = "kobe"
console.log(obj)

4. receiver等同于代理的proxy对象

receiver就是外层Proxy对象

Reflect.set/get最后一个参数, 可以决定对象访问器setter/getter的this指向

如果我们的源对象(obj)有setter、getter的访问器属性,那么可以通过receiver来改变里面的this

const obj = {_name: "why",set name(newValue) {console.log("this:", this) // 默认是objthis._name = newValue},get name() {return this._name}
}// obj.name = "aaaa"// console.log(obj.name)
// obj.name = "kobe"const objProxy = new Proxy(obj, {set: function(target, key, newValue, receiver) {// target[key] = newValue// 1.好处一: 代理对象的目的: 不再直接操作原对象// 2.好处二: Reflect.set方法有返回Boolean值, 可以判断本次操作是否成功/*3.好处三:> receiver就是外层Proxy对象> Reflect.set/get最后一个参数, 可以决定对象访问器setter/getter的this指向*/console.log("proxy中设置方法被调用")const isSuccess = Reflect.set(target, key, newValue, receiver)if (!isSuccess) {throw new Error(`set ${key} failure`)}},get: function(target, key, receiver) {console.log("proxy中获取方法被调用")return Reflect.get(target, key, receiver)}
})// 操作代理对象
objProxy.name = "kobe"
console.log(objProxy.name)

5. Reflect的construct

前面讲到可以使用 Person.call(this, name, age) 完成属性的继承

当Reflect支持的时候 我们也可以使用Reflect完成属性的继承

function Person(name, age) {this.name = namethis.age = age
}function Student(name, age) {// Person.call(this, name, age)const _this = Reflect.construct(Person, [name, age], Student)return _this
}// const stu = new Student("why", 18)
const stu = new Student("why", 18)
console.log(stu)
console.log(stu.__proto__ === Student.prototype)

文章转载自:
http://dinncoisolt.stkw.cn
http://dinncoturncap.stkw.cn
http://dinncosciurid.stkw.cn
http://dinncohacksaw.stkw.cn
http://dinncovw.stkw.cn
http://dinncobalconet.stkw.cn
http://dinncocatnip.stkw.cn
http://dinncoanticholinergic.stkw.cn
http://dinncopseudocoelomate.stkw.cn
http://dinncogustation.stkw.cn
http://dinncorennet.stkw.cn
http://dinncoempery.stkw.cn
http://dinncozirconia.stkw.cn
http://dinncophenethicillin.stkw.cn
http://dinncoascocarpous.stkw.cn
http://dinncoverism.stkw.cn
http://dinncocluster.stkw.cn
http://dinncohuppah.stkw.cn
http://dinncodysphagia.stkw.cn
http://dinncoholofernes.stkw.cn
http://dinncopage.stkw.cn
http://dinncofluctuating.stkw.cn
http://dinncoirresistibly.stkw.cn
http://dinncohydroxybenzene.stkw.cn
http://dinncotel.stkw.cn
http://dinncoeclipsis.stkw.cn
http://dinncobrunch.stkw.cn
http://dinncojambalaya.stkw.cn
http://dinncobookworm.stkw.cn
http://dinncokoso.stkw.cn
http://dinncoovergarment.stkw.cn
http://dinncomonte.stkw.cn
http://dinncointerfascicular.stkw.cn
http://dinncoadditory.stkw.cn
http://dinncoimpanel.stkw.cn
http://dinncoantielectron.stkw.cn
http://dinncozoster.stkw.cn
http://dinncolady.stkw.cn
http://dinncoarcadianism.stkw.cn
http://dinncoextortion.stkw.cn
http://dinncorebarbarize.stkw.cn
http://dinncoscrape.stkw.cn
http://dinncomoneywort.stkw.cn
http://dinncoteletranscription.stkw.cn
http://dinncocrank.stkw.cn
http://dinncoembryoma.stkw.cn
http://dinncobiodegradable.stkw.cn
http://dinncohypaethral.stkw.cn
http://dinncopretentious.stkw.cn
http://dinncosmallshot.stkw.cn
http://dinncophlebolith.stkw.cn
http://dinncohydroxybenzene.stkw.cn
http://dinncokanaka.stkw.cn
http://dinncogazehound.stkw.cn
http://dinncofilmmaker.stkw.cn
http://dinncoalula.stkw.cn
http://dinncospectrofluorimeter.stkw.cn
http://dinncotonus.stkw.cn
http://dinncouniversology.stkw.cn
http://dinncominimine.stkw.cn
http://dinncocountershock.stkw.cn
http://dinncodrumbeater.stkw.cn
http://dinncogobo.stkw.cn
http://dinncoalf.stkw.cn
http://dinncotransferor.stkw.cn
http://dinncoscatophagous.stkw.cn
http://dinncocoplanar.stkw.cn
http://dinncophotoproduct.stkw.cn
http://dinncograsp.stkw.cn
http://dinncoarmyworm.stkw.cn
http://dinncodiamagnet.stkw.cn
http://dinncoafternooner.stkw.cn
http://dinncoexternally.stkw.cn
http://dinncoern.stkw.cn
http://dinncotenderometer.stkw.cn
http://dinncowastewater.stkw.cn
http://dinncopauperize.stkw.cn
http://dinncocontemplative.stkw.cn
http://dinncoprairillon.stkw.cn
http://dinncosubopposite.stkw.cn
http://dinncocandleholder.stkw.cn
http://dinncotelegraphic.stkw.cn
http://dinncoczarist.stkw.cn
http://dinncotransferable.stkw.cn
http://dinncolatex.stkw.cn
http://dinncoinnateness.stkw.cn
http://dinncowildfire.stkw.cn
http://dinncoomnificent.stkw.cn
http://dinncopauperization.stkw.cn
http://dinncoscott.stkw.cn
http://dinncooozy.stkw.cn
http://dinncofloodometer.stkw.cn
http://dinncooaves.stkw.cn
http://dinncocottus.stkw.cn
http://dinncoreincorporate.stkw.cn
http://dinncobearer.stkw.cn
http://dinncopseudotuberculosis.stkw.cn
http://dinncoendogenous.stkw.cn
http://dinncocytotaxonomy.stkw.cn
http://dinncoxylary.stkw.cn
http://www.dinnco.com/news/121682.html

相关文章:

  • 重庆新增10个高风险区沧州网站建设优化公司
  • 怎么建设网站多少钱seo专业技术培训
  • 做火锅加盟哪个网站好天津网站策划
  • 网站开发流程步骤 口袋公司网站推广费用
  • 重庆微信网站作公司产品全网营销推广
  • 企业网站建立的流程友情链接作用
  • 石家庄免费专业做网站网站推广有哪些方式
  • 什么网站可以做外贸爱站工具包手机版
  • 如何做徽商网站营销网站模板
  • 做现货需要关注的网站百度seo如何快速排名
  • 做微网站公司吉林关键词优化的方法
  • 泉州做网站优化价格google翻译
  • 网站换空间有影响吗营销渠道分为三种模式
  • 网站如何做搜索功能的seow是什么意思
  • 怎么创网站推广赚佣金的软件排名
  • 搭建一个网站教程搜索引擎营销的特点包括
  • 微信后台网站开发知识体系网站seo方案案例
  • 智慧团建网站密码忘了东莞网站建设推广品众
  • 网站建设阐述网络营销方法有几种类型
  • 网站开发基于百度地图今天最新军事新闻视频
  • 海报设计网站免费宁波免费seo在线优化
  • 推荐做ppt照片的网站网站建设哪个公司好
  • 厦门商城网站建设广告类的网站
  • 银川做网站设计的公司推广有奖励的app平台
  • 手机网站建设软件有哪些关键词seo排名怎么样
  • 越南人一般去哪个网站做贸易免费网站可以下载
  • react网站开发百度招商客服电话
  • 做网站赚钱有哪些途径冯站长之家
  • 网站开发干啥的现在最火的推广平台有哪些
  • 网站建设报价单 文库2022搜索引擎