当前位置:网站首页>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 ');边栏推荐
- Typescript防止基类被实例化
- 1.8tft color screen test code (stm32f407ve)
- 解决控制文件全部损坏的异常
- Advanced C language: pointer (3)
- 无代码开发平台通讯录入门教程
- [physical application] atmospheric absorption loss with matlab code
- Stm32f4 serial port burning [flymcu]
- 【MySQL系列】 MySQL表的增删改查(进阶)
- Applet, JS, transfer object jump transfer parameter problem
- Kotlin JVM annotation
猜你喜欢

Submission records of frontiers Publishing House (with status changes)

【C语言】三子棋小游戏实现
![Stm32f4 serial port burning [flymcu]](/img/5b/0e35c3c58354f911631a3affd3909b.png)
Stm32f4 serial port burning [flymcu]

参加竞赛同学们的留言 : 第十七届的记忆

【MySQL系列】 MySQL表的增删改查(进阶)

Record a question about the order of trigonometric function exchange integrals

WebView optimization

MySQL常用的日期时间函数

Servlet的使用手把手教学(一)

华为无线设备配置利用WDS技术部署WLAN业务
随机推荐
Swift type attribute and its attentions
High quality subroutine 1
二叉搜索树
DirectX修复工具下载(exagear模拟器数据包在哪里)
Thesis reading (3) - googlenet of classification
Sqlilabs-2 (breakthrough record)
【物理应用】大气吸收损耗附matlab代码
Will Qualcomm and MediaTek chips soon be sold, and will they surpass Huawei to become the first in China?
RouterOS 有限dns劫持及check
There are four ways for Nacos to configure hot updates and multiple ways to read project configuration files, @value, @refreshscope, @nacosconfigurationproperties
Servlet的使用手把手教学(一)
Vant web app installation reference
《MySQL数据库进阶实战》读后感(SQL 小虚竹)
18张图,直观理解神经网络、流形和拓扑
Seagate released a new risc-v architecture processor: the performance of mechanical hard disk soared 3 times
DirectX repair tool download (where is exagear simulator package)
cnpm安装步骤
Wheel 6: qserialport serial port data transceiver
[C language] implementation of three piece chess games
无代码开发平台通讯录导出入门教程