当前位置:网站首页>ES6中的代理proxy
ES6中的代理proxy
2022-07-01 18:45:00 【SignalFire】
Proxy:代理器,改变对象的默认访问行为;在对象的访问行为之前增设拦截,通过Proxy实例进行的访问行为都必须经过设置的拦截;在拦截中可以根据需要 设置拦截条件
p()为自定义函数,后文中的p()都是调用这个函数
let p = function(){
console.log(...arguments);
}
目录
拦截器
| 拦截器 | 作用 |
| get | 拦截对象属性的读取,如obj.key,obj['key'],返回属性值 |
| set | 拦截属性的设置,返回布尔值 |
| has | 拦截key in obj的操作,返回布尔值 |
| ownKeys | 拦截遍历如: for in 循环 |
一、数组代理
1、拦截获取值的操作
2、拦截设置操作,给数组赋值或者添加元素时触发如:arr.push(xxx),arr[0] = xxx
let arr = [1,2,3,4,5];
arr = new Proxy(arr,{
//如果元素存在返回值,否则返回error
get(target,prop){//target为代理的对象,此处为数组arr;prop此处为索引
return prop in target ? target[prop] : 'error';
},
//设置值时触发
set(target,prop,val){
if(typeof val === 'number'){//是数值类型添加,否则报错,
target[prop] = val;//可根据实际需要修改条件
return true;
}else return false;
}
})
二、对象代理
1、拦截是否存在如in
let age = {
min:0,
max:143
}
age = new Proxy(age,{
has(target,prop){
return prop >= target.min && prop <= target.max;
}
})
p(23 in age);
p(144 in age);
2、拦截遍历如:
for in 循环
Object.getOwnPropertyNames(obj);
Object.getOwnPropertySymbols(obj);
Object.keys(obj);
let user = {
name:"Asia",
age:23,
_password:'666'
}
user = new Proxy(user,{
ownKeys(target){//将对象的所有键中以'_'开头的键过滤掉,此处过滤了密码属性
return Object.keys(target).filter(key => !key.startsWith('_'));//可根据需要自定义拦截条件
}
});
for(let key in user){
p(key);
}
3、拦截某些属性一切操作
let u = {
name:'Asia',
age:23,
_password:'666'
}
u = new Proxy(u,{
set(obj,key,val){
if(key.startsWith('_')){
throw new Error('不可修改');
}else{
obj[key] = val;
}
},
get(obj,key){
if(key.startsWith('_')){
throw new Error('不可访问');
}else{
return obj[key];
}
},
deleteProperty(obj,key){
if(key.startsWith('_')){
throw new Error('不可删除');
}else{
delete obj[key];
return true;
}
},
ownKeys(obj,key){
return Object.keys(obj).filter(key => !key.startsWith('_'));
}
})
try{
u._password = '000';
}catch(err){
p(err.message);
};
try{
p(u._password);
}catch(err){
p(err.message);
}
try{
delete u._password;
}catch(err){
p(err.message);
}
Object.keys(u).forEach(key => p(u[key]));
三、函数代理
改变函数返回值
let sum = (...args) => {
let sum = 0;
args.forEach(item => {
sum += item;
});
return sum;
}
sum = new Proxy(sum,{
apply(fn,ctx,args){
return fn(...args) * 10;
}
});
p(sum(1,2,3));//60
p(sum.call(null,1,2,3));//60
p(sum.apply(null,[1,2,3]));//60
四、class代理
对new进行拦截
let User = class{
constructor(name){
this.name = name;
}
}
User = new Proxy(User,{
construct(cls,args,newCls){
p('construct 正在拦截');
return new cls(...args);
}
});
p(new User('Asia'));
五、ES5中的代理
Object.definePrototype(obj,属性名,callback);
let obj = {};
let _name = '';
Object.definePrototype(obj,'name',{
set(val){
_name = val;
},
get(){
return _name;
}
})
边栏推荐
- indexof和includes的区别
- ECS summer money saving secret, this time @ old users come and take it away
- 简版拼多多商品数据
- Digital business cloud: from planning to implementation, how does Minmetals Group quickly build a new pattern of digital development?
- Supervarimag superconducting magnet system SVM series
- SuperOptiMag 超导磁体系统 — SOM、SOM2 系列
- transform + asm资料
- DTD建模
- 精耕渠道共謀發展 福昕攜手偉仕佳傑開展新產品培訓大會
- C-end dream is difficult to achieve. What does iFLYTEK rely on to support the goal of 1billion users?
猜你喜欢

SuperVariMag 超导磁体系统 — SVM 系列

Lumiprobe free radical analysis h2dcfda instructions

Junit单元测试框架详解

【To .NET】C#集合类源码解析

Lake Shore低温恒温器的氦气传输线

MATLAB中subplot函数的使用
![[pytorch record] automatic hybrid accuracy training torch cuda. amp](/img/a5/cf1eb2801380cf2887dfd532d3eb1e.jpg)
[pytorch record] automatic hybrid accuracy training torch cuda. amp

Case sharing: basic networking configuration of QinQ

Solution of digital supply chain centralized purchase platform in mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase

Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency
随机推荐
从零开始学 MySQL —数据库和数据表操作
[6.24-7.1] review of wonderful technical blog posts in the writing community
论文阅读【Discriminative Latent Semantic Graph for Video Captioning】
Netease games, radical going to sea
Solidity - 算术运算的截断模式(unchecked)与检查模式(checked)- 0.8.0新特性
The use of subplot function in MATLAB
[live broadcast appointment] database obcp certification comprehensive upgrade open class
论文阅读【Learning to Discretely Compose Reasoning Module Networks for Video Captioning】
English语法_形容词/副词3级 -注意事项
狼人杀攻略:你当我好骗吗,我们相信谁!
Three ways for redis to realize current limiting
【英语语法】Unit1 冠词、名词、代词和数词
The former 4A executives engaged in agent operation and won an IPO
微信公众号开发相关流程及功能介绍
The intelligent epidemic prevention system provides safety guarantee for the resumption of work and production at the construction site
CDGA|从事通信行业,那你应该考个数据管理证书
More information about M91 fast hall measuring instrument
Case sharing: basic networking configuration of QinQ
Instagram 为何从内容共享平台变成营销工具?独立站卖家如何利用该工具?
智慧防疫系统为建筑工地复工复产提供安全保障