当前位置:网站首页>Typescript反射对象Reflect使用
Typescript反射对象Reflect使用
2022-07-25 18:38:00 【RemoteDev】
let tmpProxy = new Proxy({},{
set:function (t,p,v,r){
let s = Reflect.set(t,p,v,r);
if(s){
console.log('set:',t,p,v);
}
return s;
},
get:function (t,p){
console.log('get:',t,p);
return Reflect.get(t,p);
},
deleteProperty(t, p) {
console.log('delete:',t,p);
return Reflect.deleteProperty(t,p);
},
has(t, p) {
console.log('has:',t,p);
return Reflect.has(t,p);
}
});
tmpProxy.x=0;
console.log(tmpProxy.x);
delete tmpProxy.x;
tmpProxy.hasOwnProperty('x');
'x' in tmpProxy;
let obj = {
name:'RemoteDev',
job:'IT'
};
console.log(Reflect.get(obj,'name'));
console.log(Reflect.get(obj,'job'));
//绑定接收者
let bindObj={
x:1,
y:2,
get getXY(){
return this.x + this.y;
}
};
//接收者
let recv = {
x:20,
y:30
}
//调用
let r =Reflect.get(bindObj,'getXY',recv);
console.log(r);
let setObj={
x:1,
y:2,
set setXY({x,y}){
this.x=x;
this.y=y;
}
};
Reflect.set(setObj,'setXY',{x:50,y:60});
console.log(setObj.x,setObj.y);
//Reflect的set方法会触发代理器的defineProperty拦截
let obj1 = {
x:1
};
let handler={
set(t,p,v,r){
console.log('set call');
Reflect.set(t,p,v,r);
},
defineProperty(t, k, d) {
console.log('defineProperty call');
Reflect.defineProperty(t,k,d);
}
};
// @ts-ignore
let proxy1= new Proxy(obj1,handler);
proxy1.x=88;
Reflect.deleteProperty(obj1,'x');
class A{
constructor(x) {
Object.assign(this,{x:0});
}
func (x){
this.x = x;
console.log("a func");
}
}
let func_inst1 = new A(7);
console.log(func_inst1.func);
let func_inst2 = Reflect.construct(A,[100]);
console.log(func_inst2.func);
console.log(A.prototype,Reflect.getPrototypeOf(func_inst1));
let proto = {y:5};
Reflect.setPrototypeOf(func_inst1,proto);
console.log(Reflect.getPrototypeOf(func_inst1));
let arr = [1,2,3,4,5,6,7,8,9,0];
let tMin = Reflect.apply(Math.min,Math,arr);
let tMax = Reflect.apply(Math.max,Math,arr);
let tType = Reflect.apply(Object.prototype.toString,tMin,[]);
console.log(tMin,tMax,tType);
let newObj = {x:1};
Reflect.defineProperty(newObj,'newProp',{value:123});
console.log(Reflect.get(newObj,'newProp'));
console.log(Reflect.getOwnPropertyDescriptor(newObj,'newProp'));
console.log(Reflect.isExtensible(newObj));
Reflect.preventExtensions(newObj);
console.log(Reflect.isExtensible(newObj));
console.log(Reflect.ownKeys(newObj));
//观察者模式
let queueObserve = new Set();//观察池
let observe = fn=>queueObserve.add(fn);//观察者
let observable=data=>new Proxy(data,{set});//数据触发者,通过代理器实现
function set(t,k,v,r) {
let d = Reflect.set(t,k,v,r);
// @ts-ignore
queueObserve.forEach(v=>v());//遍历池并调用池中方法
return d;
}
//观察数据对象
let data=observable({
name:'RemoteDev',
job:'IT'
});
function print() {
console.log(`${data.name},${data.job}`);
}
observe(print);//观察者
data.name='RM';//数据发生变化,调用observable--->set--->遍历池
边栏推荐
- 【帮助中心】为您的客户提供自助服务的核心选项
- Repair process of bad blocks of primary standby database
- Thales launches solutions to help SAP customers control cloud data
- Practice of RTC performance automation tool in memory optimization scenario
- Flexible current probe selection guide
- 推特收购舆论战,被马斯克变成了小孩吵架
- win11下vscode 自动升级失败 There was an error while marking a file for deletion
- Paper revision reply 1
- 《21天精通TypeScript-4》-类型推断与语义检查
- There was an error while marking a file for deletion
猜你喜欢

Esp32 S3 vscode+idf setup

Register carefully! The number of applicants for these double non-governmental institutions exceeded 10000!

1--- electronic physical cognition

曾拿2亿融资,昔日网红书店如今全国闭店,60家店仅剩3家

How to build an enterprise level OLAP data engine for massive data and high real-time requirements?

pd.melt() vs reshape2::melt()

SQL things

市值300亿,欧洲十年来最大IPO再冲纽交所

Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree

什么是3DE体验平台
随机推荐
[NOI2015] 软件包管理器
1---电子实物认知
【帮助中心】为您的客户提供自助服务的核心选项
【网页性能优化】SPA(单页面应用)首屏加载速度慢怎么办?
如何创建一个有效的帮助文档?
Paper revision reply 1
市值300亿,欧洲十年来最大IPO再冲纽交所
韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
Yes, UDP protocol can also be used to request DNS server
uniapp滚动条置顶效果、自定义页面滚动条的位置(整理)
Circulaindicator component, which makes the indicator style more diversified
【华为机试真题】字符串匹配
15. Simple salary management system design
3DE reply
拍卖行作VC,第一次出手就投了个Web3
CircleIndicator组件,使指示器风格更加多样化
c语言---25 扫雷游戏
Ceres curve fitting
[translation] LFX 2022 spring tutor qualification opening - apply for CNCF project before February 13!