当前位置:网站首页>Typescript reflection object reflection use
Typescript reflection object reflection use
2022-07-25 18:43: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'));
// Bind recipient
let bindObj={
x:1,
y:2,
get getXY(){
return this.x + this.y;
}
};
// The receiver
let recv = {
x:20,
y:30
}
// call
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 Of set Method will trigger the agent defineProperty Intercept
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));
// Observer mode
let queueObserve = new Set();// Observation pool
let observe = fn=>queueObserve.add(fn);// The observer
let observable=data=>new Proxy(data,{set});// Data trigger , Through the agent
function set(t,k,v,r) {
let d = Reflect.set(t,k,v,r);
// @ts-ignore
queueObserve.forEach(v=>v());// Traverse the pool and call the methods in the pool
return d;
}
// Observe the data object
let data=observable({
name:'RemoteDev',
job:'IT'
});
function print() {
console.log(`${data.name},${data.job}`);
}
observe(print);// The observer
data.name='RM';// Data changes , call observable--->set---> Traversal pool
边栏推荐
- 进程间的通信(管道通信)
- Circulaindicator component, which makes the indicator style more diversified
- 如何创建一个有效的帮助文档?
- 【华为机试真题】字符串匹配
- Detailed introduction and application of GaN (comprehensive and complete)
- Typescript反射对象Reflect使用
- 给生活加点惊喜,做创意生活的原型设计师丨编程挑战赛 x 选手分享
- Optimistic lock resolution
- 信达证券是国企吗?在信达证券开户资金安全吗?
- Esp32 S3 vscode+idf setup
猜你喜欢

上半年出货量已超去年全年,森思泰克毫米波雷达“夺食”国际巨头

Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network

15. Simple salary management system design

Twitter acquired a public opinion war, which was turned into a child quarrel by musk

With a market value of 30billion yuan, the largest IPO in Europe in the past decade was re launched on the New York Stock Exchange

一周活动速递|深入浅出第8期;Meetup成都站报名进行中

Project: serial port receiving RAM storage TFT display (complete design)

Automatic machine learning library: Tpot の learning notes

VC/PE正跑向青岛

华为年内二度招聘“天才少年”;540万Twitter账号信息泄露,卖价3万美元;谷歌解雇了相信AI有意识的工程师|极客头条
随机推荐
pd.melt() vs reshape2::melt()
vim基本操作命令
Powell's function of Ceres
Jz32 print binary tree from top to bottom
「Wdsr-3」蓬莱药局 题解
7/24 训练日志
PHP 中的跨站请求伪造
关爱一线防疫工作者,浩城嘉业携手高米店街道办事处共筑公益长城
R language ggplot2 visual line, custom configuration title text related content color and legend color match (match colors of groups)
You can change this value on the server by setting the 'Max_ allowed_ Packet 'variable error
TypeError: Unrecognized value type: <class ‘str‘> ParserError: Unknown string format
Tensor to img & imge to tensor (tensor conversion of pytorch)
R language uses GT package and gtextras package to display tabular data beautifully: GT_ bar_ Plot function and GT_ plt_ bar_ PCT function visual percentage bar graph, percentage bar graph of original
这届年轻人,为“丑东西”有多上头?
Partial correlation calculation of R language and partial correlations calculation using pcor function of GGM package
[haoi2015] tree operation
给生活加点惊喜,做创意生活的原型设计师丨编程挑战赛 x 选手分享
Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
Interpretation of "cross chain interconnection smart contract"
【翻译】LFX 2022年春季导师资格开放--2月13日前申请CNCF项目!