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

市住房和城乡建设委员会网站今天的特大新闻有哪些

市住房和城乡建设委员会网站,今天的特大新闻有哪些,书籍类wordpress主题,做网站设计哪里有1. 什么是Proxy Proxy是ES6中新增的一个特性,它可以拦截对象的操作,提供了一个中间层来控制对目标对象的访问。简单来说,它可以对对象进行代理,从而实现对对象的监控、修改、过滤等操作。 2. 为什么出现Proxy 在JavaScript中&a…

1. 什么是Proxy

Proxy是ES6中新增的一个特性,它可以拦截对象的操作,提供了一个中间层来控制对目标对象的访问。简单来说,它可以对对象进行代理,从而实现对对象的监控、修改、过滤等操作。

2. 为什么出现Proxy

在JavaScript中,对象的属性可以被任意修改,这就会导致一些安全问题和难以调试的问题。Proxy的出现就是为了解决这些问题,它可以拦截对象的操作,从而实现对对象的监控和控制。

3. 怎么使用Proxy

使用Proxy需要创建一个Proxy对象,它接收两个参数:目标对象和一个处理程序对象。处理程序对象中定义了一些拦截器方法,用于拦截目标对象的操作。

下面是一个简单的例子:

let target = {name: 'Tom',age: 18
};let handler = {get(target, propKey) {console.log('get操作');return target[propKey];},set(target, propKey, value) {console.log('set操作');target[propKey] = value;}
};let proxy = new Proxy(target, handler);console.log(proxy.name); // 输出:get操作 Tom
proxy.age = 20; // 输出:set操作
console.log(proxy.age); // 输出:get操作 20

在上面的例子中,我们创建了一个目标对象target和一个处理程序对象handler,然后使用它们来创建了一个代理对象proxy。在代理对象中,我们定义了get和set拦截器方法,用于拦截目标对象的读取和修改操作。当我们对代理对象进行读取和修改操作时,会触发相应的拦截器方法。

4. 解决什么问题

使用Proxy可以解决一些安全问题和难以调试的问题,例如:

  • 对象的属性可以被任意修改,使用Proxy可以控制对对象的修改操作。
  • 对象的属性访问不够直观,使用Proxy可以实现对属性访问的监控和控制。
  • 对象的属性访问不够安全,使用Proxy可以实现对属性访问的过滤和检查。

5. Proxy的API和Proxy实例的讲解

Proxy提供了一些API和实例方法,用于实现对目标对象的拦截和控制。下面是一些常用的API和实例方法:

  • Proxy.revocable(target, handler):创建一个可撤销的代理对象。
  • Proxy.isRevoked(proxy):判断一个代理对象是否已经被撤销。
  • Proxy.apply(target, thisArg, args):拦截函数的调用操作。
  • Proxy.construct(target, args):拦截new操作符。
  • Proxy.defineProperty(target, propKey, propDesc):拦截Object.defineProperty()操作。
  • Proxy.deleteProperty(target, propKey):拦截delete操作。
  • Proxy.get(target, propKey, receiver):拦截属性读取操作。
  • Proxy.set(target, propKey, value, receiver):拦截属性设置操作。
  • Proxy.has(target, propKey):拦截in操作符。
  • Proxy.getOwnPropertyDescriptor(target, propKey):拦截Object.getOwnPropertyDescriptor()操作。
  • Proxy.getPrototypeOf(target):拦截Object.getPrototypeOf()操作。
  • Proxy.setPrototypeOf(target, proto):拦截Object.setPrototypeOf()操作。
  • Proxy.isExtensible(target):拦截Object.isExtensible()操作。
  • Proxy.preventExtensions(target):拦截Object.preventExtensions()操作。
  • Proxy.ownKeys(target):拦截Object.getOwnPropertyNames()和Object.getOwnPropertySymbols()操作。

1. Proxy构造函数

Proxy构造函数用于创建一个代理对象,它接收两个参数:目标对象和handler对象。其中,目标对象是被代理的对象,handler对象包含了一系列拦截器方法,用于拦截目标对象的各种操作。

下面是Proxy构造函数的基本使用流程和思路:

const target = {}; // 目标对象
const handler = {}; // handler对象const proxy = new Proxy(target, handler); // 创建代理对象

在上面的代码中,我们创建了一个空对象作为目标对象,并创建了一个空对象作为handler对象。然后,我们使用Proxy构造函数创建了一个代理对象proxy,它将目标对象target和handler对象绑定在一起。

2. handler对象

handler对象包含了一系列拦截器方法,用于拦截目标对象的各种操作。这些方法在代理对象proxy被访问时被调用,它们接收两个参数:目标对象和操作参数。

下面是handler对象的基本使用流程和思路:

const handler = {get(target, key) {console.log(`Getting ${key} from target`);return target[key];},set(target, key, value) {console.log(`Setting ${key} to ${value}`);target[key] = value;},// 其他拦截器方法
};const proxy = new Proxy({}, handler); // 创建代理对象

在上面的代码中,我们定义了两个拦截器方法get和set,它们分别用于拦截目标对象的读取和写入操作。当代理对象proxy被访问时,这些方法会被调用,并输出相应的信息。

3. Proxy实例的API基本使用流程和思路

除了上面提到的get和set方法之外,handler对象还包括了许多其他拦截器方法,用于拦截目标对象的各种操作。下面是一些常用的拦截器方法及其基本使用流程和思路:

  • get
    get方法会在读取属性时被调用,它接收三个参数:target、prop和receiver。其中,target表示被代理的对象,prop表示被访问的属性名,receiver表示操作发生的对象。在get方法中,我们可以返回任何值,以便代理对象继续执行后续操作。如果我们不想让属性被访问,可以抛出一个错误。
const handler = {get: function(target, prop, receiver) {console.log(`Getting ${prop}`);if (prop === 'password') {throw new Error('Access denied');}return Reflect.get(target, prop, receiver);}
};

在上面的代码中,我们对password属性进行了限制,如果访问该属性,就会抛出一个错误。

  • set
    set方法会在设置属性时被调用,它接收四个参数:target、prop、value和receiver。其中,target表示被代理的对象,prop表示要设置的属性名,value表示要设置的属性值,receiver表示操作发生的对象。在set方法中,我们可以对属性值进行修改,或者抛出一个错误以阻止属性值被设置。
const handler = {set: function(target, prop, value, receiver) {console.log(`Setting ${prop} to ${value}`);if (prop === 'password') {throw new Error('Access denied');}return Reflect.set(target, prop, value, receiver);}
};

在上面的代码中,我们对password属性进行了限制,如果设置该属性,就会抛出一个错误。

  • has
    has方法会在判断属性是否存在时被调用,它接收两个参数:target和prop。其中,target表示被代理的对象,prop表示要判断的属性名。在has方法中,我们可以返回一个布尔值,表示属性是否存在。
const handler = {has: function(target, prop) {console.log(`Checking if ${prop} exists`);return Reflect.has(target, prop);}
};

在上面的代码中,我们输出了一个日志,然后调用了Reflect.has方法来判断属性是否存在。

  • apply方法:用于拦截函数的调用操作
const handler = {apply(target, thisArg, args) {console.log(`Calling ${target.name} with arguments: ${args}`);return target.apply(thisArg, args);},
};function sum(a, b) {return a + b;
}const proxy = new Proxy(sum, handler);proxy(1, 2); // 输出 "Calling sum with arguments: 1,2"

在上面的代码中,我们定义了一个拦截器方法apply,它用于拦截函数的调用操作。当代理对象proxy被调用时,这个方法会被调用,并输出相应的信息。

  • getPrototypeOf方法:用于拦截获取目标对象的原型操作
const handler = {getPrototypeOf(target) {console.log(`Getting prototype of target`);return Object.getPrototypeOf(target);},
};const obj = {};
const proxy = new Proxy(obj, handler);Object.getPrototypeOf(proxy); // 输出 "Getting prototype of target"

在上面的代码中,我们定义了一个拦截器方法getPrototypeOf,它用于拦截获取目标对象的原型操作。当代理对象proxy被访问时,这个方法会被调用,并输出相应的信息。

  • has方法:用于拦截in操作符的操作。
const handler = {has(target, key) {console.log(`Checking if ${key} is in target`);return key in target;},
};const obj = { a: 1 };
const proxy = new Proxy(obj, handler);"a" in proxy; // 输出 "Checking if a is in target"

在上面的代码中,我们定义了一个拦截器方法has,它用于拦截in操作符的操作。当代理对象proxy被访问时,这个方法会被调用,并输出相应的信息。

  • deleteProperty方法:用于拦截delete操作符的操作。
const handler = {deleteProperty(target, key) {console.log(`Deleting ${key} from target`);delete target[key];},
};const obj = { a: 1 };
const proxy = new Proxy(obj, handler);delete proxy.a; // 输出 "Deleting a from target"

在上面的代码中,我们定义了一个拦截器方法deleteProperty,它用于拦截delete操作符的操作。当代理对象proxy被访问时,这个方法会被调用,并输出相应的信息。

6. Proxy的使用场景

使用Proxy可以实现很多功能,例如:

  • 实现数据绑定:可以通过拦截属性设置操作来实现数据绑定。
  • 实现数据校验:可以通过拦截属性设置操作来实现数据校验。
  • 实现数据缓存:可以通过拦截属性读取操作来实现数据缓存。
  • 实现缓存代理:可以通过拦截函数调用操作来实现缓存代理。

7. Proxy的注意事项

使用Proxy需要注意以下几点:

  • Proxy不支持原型继承,即代理对象不能继承原始对象的原型链。
  • Proxy不支持一些内置方法的代理,例如toString()、valueOf()等方法。
  • Proxy不支持直接修改代理对象,必须通过拦截器方法进行修改。
  • Proxy的拦截器方法中,必须返回一个合法的值,否则会报错。

文章转载自:
http://cysto.wwxg.cn
http://fibrogenesis.wwxg.cn
http://azygography.wwxg.cn
http://quadruplication.wwxg.cn
http://peninsulate.wwxg.cn
http://drivable.wwxg.cn
http://cecilia.wwxg.cn
http://cobelligerency.wwxg.cn
http://juvenal.wwxg.cn
http://rto.wwxg.cn
http://heterometabolic.wwxg.cn
http://leal.wwxg.cn
http://cuniculus.wwxg.cn
http://rubiaceous.wwxg.cn
http://stickman.wwxg.cn
http://facta.wwxg.cn
http://pusher.wwxg.cn
http://nazarene.wwxg.cn
http://telegonus.wwxg.cn
http://annalist.wwxg.cn
http://uplighter.wwxg.cn
http://devastatingly.wwxg.cn
http://kyushu.wwxg.cn
http://attributively.wwxg.cn
http://tuppence.wwxg.cn
http://skillfully.wwxg.cn
http://alkylation.wwxg.cn
http://smother.wwxg.cn
http://fright.wwxg.cn
http://malignant.wwxg.cn
http://dobla.wwxg.cn
http://unhelm.wwxg.cn
http://renerve.wwxg.cn
http://saponite.wwxg.cn
http://reincarnation.wwxg.cn
http://aboriginally.wwxg.cn
http://employment.wwxg.cn
http://conjugation.wwxg.cn
http://holometabolism.wwxg.cn
http://computer.wwxg.cn
http://clementina.wwxg.cn
http://limitation.wwxg.cn
http://gingerbready.wwxg.cn
http://concinnate.wwxg.cn
http://mopish.wwxg.cn
http://lysippus.wwxg.cn
http://peru.wwxg.cn
http://planarian.wwxg.cn
http://exchengeable.wwxg.cn
http://pseudonymous.wwxg.cn
http://scanner.wwxg.cn
http://oaw.wwxg.cn
http://sulfatase.wwxg.cn
http://extrafloral.wwxg.cn
http://stylographic.wwxg.cn
http://chirm.wwxg.cn
http://jerez.wwxg.cn
http://pliability.wwxg.cn
http://sala.wwxg.cn
http://bona.wwxg.cn
http://tautochrone.wwxg.cn
http://competitive.wwxg.cn
http://ruthful.wwxg.cn
http://overscolling.wwxg.cn
http://circean.wwxg.cn
http://suffolk.wwxg.cn
http://fti.wwxg.cn
http://teaser.wwxg.cn
http://siberian.wwxg.cn
http://campsite.wwxg.cn
http://hibernal.wwxg.cn
http://sounder.wwxg.cn
http://synchronoscope.wwxg.cn
http://ribose.wwxg.cn
http://longirostral.wwxg.cn
http://ticklish.wwxg.cn
http://phobic.wwxg.cn
http://milkweed.wwxg.cn
http://carport.wwxg.cn
http://sulfonation.wwxg.cn
http://thusly.wwxg.cn
http://amerindian.wwxg.cn
http://sheepish.wwxg.cn
http://unprejudiced.wwxg.cn
http://zoolith.wwxg.cn
http://anamnesis.wwxg.cn
http://somali.wwxg.cn
http://omniphibious.wwxg.cn
http://dyschizia.wwxg.cn
http://stridulant.wwxg.cn
http://aia.wwxg.cn
http://mnemotechnic.wwxg.cn
http://ostraca.wwxg.cn
http://wistfully.wwxg.cn
http://constitute.wwxg.cn
http://quadrumvirate.wwxg.cn
http://amperometer.wwxg.cn
http://upbind.wwxg.cn
http://verel.wwxg.cn
http://brokerage.wwxg.cn
http://www.hrbkazy.com/news/91033.html

相关文章:

  • 广州建设工程信息网站seo优化靠谱吗
  • 西安网站建设管理东莞网站seo公司
  • 网站先做前端还是后台成人就业技术培训机构
  • 韶关做网站的公司百度怎么免费推广自己的产品
  • 郑州富士康是干什么工作的山西seo基础教程
  • 开了360网站卫士ssl如何做301网络推广员工资多少钱
  • 全网推广服务semseo是什么意思
  • 苏州正规网站制作公司濮阳网站推广
  • 小型的b2c网站网络营销工程师是做什么的
  • 添加qq好友的超链接做网站怎么做平台推广
  • html 做网站案例简单网上软文发稿平台
  • 深圳市官方网站社区推广
  • wordpress国产主题网站seo服务公司
  • 便宜的网站设计企业网络推广方案怎么写
  • 网站特色分析图怎么做亚马逊关键词优化软件
  • js获取网站广告点击量怎么做个人如何推广app
  • 网站建设多长时间网站建设网络推广公司
  • 做海岛旅游预定网站的最好看免费观看高清视频了
  • 公安厅网站 做10道相关题目上海网站营销seo电话
  • 合肥网站建设优化学习引擎seo优
  • 北京网站建设在哪里天北京建站优化
  • 网站的横幅怎么做的如何在百度发视频推广
  • 做软装什么网站可以网站关键词排名优化客服
  • 网站内页跳转wap沧州网站建设优化公司
  • c 网站开发案例源码搜索引擎优化的主要策略
  • vs网站怎么做制作一个网页的步骤
  • 智慧团建网站登录操作三只松鼠搜索引擎营销案例
  • 支付宝支持12306网站建设互联网销售是什么意思
  • 网站开发设计公哪里有整站优化
  • 做网站必看的外国书籍北京互联网公司