当前位置:网站首页>Typescript class method this pointer binding
Typescript class method this pointer binding
2022-07-28 23:19:00 【RemoteDev】
// Class this Pointer use
class Log{
constructor() {
// by printX Function binding current class object , Solve the call this.print I can't find it. print Method
this.printX = this.printX.bind(this);
// Make this Pointer auto binding method
this.printY = ((y='y') => {
this.print(`this allow auto bind this,${y}`);
});
}
print(obj){
console.log(obj);
}
printX(x='a'){
this.print(`hi,${x}`);// Used this The pointer
}
printY(x='a'){
this.print(`bind test :,${x}`);// Used this The pointer
}
}
let l = new Log();
l.printX('HELLO');// It can be called normally
// Export form call error
const {printX,printY}=l;// Export from object
//printX Called print No way to find ,this invalid
//printX('WOW');//Cannot read properties of undefined (reading 'print')
// By binding this Form solution , Function binding is added to the constructor above this object
console.log('printX Method is bound this The pointer , Problem solving ');
printX('WOW');
console.log('printY Method is automatically bound by arrow function this The pointer , Problem solving ');
printY('===world===');
// Automatically bind when getting function methods through the proxy this
function proxyAutoBindThis(obj){
let c = new WeakMap();// cache
// The agent handles events
let h = {
get (t,k){
let v = Reflect.get(t,k);
if (typeof v!=='function'){return v;}// It's not a function that doesn't handle , Go straight back to
if (!c.has(v)){c.set(v,v.bind(t));}// Do not bind repeatedly
return c.get(v);// Return bound this Function of
}
};
let p = new Proxy(obj,h);// For the incoming obj Set up agent
return p;// Return the agent object
}
class A{
test(obj){
console.log(obj);
}
test1(obj=null){
this.test(`${obj}===>,test1`);// Use this The pointer
}
test2(obj=null){
this.test(`${obj}===>,test2`);// Use this The pointer
}
}
let a = new A();// Instantiation A object a
let objBindThisByProxy = proxyAutoBindThis(a);// All methods are automatically bound this
let {test1,test2}=objBindThisByProxy;// Export from agent
// Class object call
a.test1(' Class object call ');
a.test2(' Class object call ');
// Call through the agent
test1(' Call through the agent ');
test2(' Call through the agent ');
边栏推荐
- Thesis reading (1) - zfnet of classification
- Anr questions often asked in Android interviews
- DirectX修复工具下载(exagear模拟器数据包在哪里)
- can‘t convert cuda:0 device type tensor to numpy. Use Tensor. cpu() to copy the tensor to host memory
- Hbuilderx shortcut key
- Introduction to original code, inverse code and complement code
- Wheel 7: TCP client
- 【物理应用】水下浮动风力涡轮机的尾流诱导动态模拟风场附matlab代码
- After reading MySQL database advanced practice (SQL xiaoxuzhu)
- Xinhuazhang announced the completion of more than 200million a-round financing and the comprehensive layout of eda2.0 R & D
猜你喜欢
Target detection notes - overview and common data sets
【物理应用】水下浮动风力涡轮机的尾流诱导动态模拟风场附matlab代码
MySQL foundation - data query
Thesis reading (0) - alexnet of classification
Thesis reading (3) - googlenet of classification
[MySQL series] addition, deletion, modification and query of MySQL tables (Advanced)
CGLIb 创建代理
It's settled! All products of Nezha s will be launched on July 31
MySQL数据库的基本概念以及MySQL8.0版本的部署(一)
Thesis reading (1) - zfnet of classification
随机推荐
Wheel 6: qserialport serial port data transceiver
芯华章宣布完成超2亿A轮融资,全面布局EDA2.0研发
MySQL常用的日期时间函数
After reading MySQL database advanced practice (SQL xiaoxuzhu)
二叉搜索树
pgbench基准测试《postgresql》
mgr.exe病毒导致启动程序启动失败
Introduction to original code, inverse code and complement code
leetcode 199. 二叉树的右视图
华为无线设备配置利用WDS技术部署WLAN业务
Pgbench benchmark PostgreSQL
Target detection notes -yolo
Thesis reading (3) - googlenet of classification
Introduction to address book export without code development platform
Applet, JS, transfer object jump transfer parameter problem
can‘t convert cuda:0 device type tensor to numpy. Use Tensor. cpu() to copy the tensor to host memory
GCD implementation and arc, blocks, GCD usage examples
Mgr.exe virus caused the startup program to fail
【MongoDB】MongoDB数据库的基础使用,特殊情况以及Mongoose的安装和创建流程(含有Mongoose固定版本安装)
Symbol symbol type