当前位置:网站首页>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 ');边栏推荐
- Learning experience sharing 5: yolov5 dataset division and Yolo format conversion
- RuntimeError: set_ sizes_ contiguous is not allowed on a Tensor created from .data or .detach().
- High quality subroutine 1
- 《MySQL数据库进阶实战》读后感(SQL 小虚竹)
- 芯华章宣布完成超2亿A轮融资,全面布局EDA2.0研发
- GCD implementation and arc, blocks, GCD usage examples
- 一种分布式深度学习编程新范式:Global Tensor
- Is 1E3 a floating point number?
- NPM run dev, automatically open the browser after running the project
- CGLIb 创建代理
猜你喜欢
![[radar] radar signal online sorting based on kernel clustering with matlab code](/img/56/1f8e8690b47fc4a1f101d4e530b87f.png)
[radar] radar signal online sorting based on kernel clustering with matlab code

以价换量却遭遇销量“六连跌”,不再安全的沃尔沃还能翻身吗?

sql优化常用的几种方法

A new paradigm of distributed deep learning programming: Global tensor

6 个超级良心的开源教程!

The functions and differences of display, visibility and overflow

6 open source tutorials of super conscience!

Basic concept of MySQL database and deployment of MySQL version 8.0 (I)

can‘t convert cuda:0 device type tensor to numpy. Use Tensor. cpu() to copy the tensor to host memory

xshell7,xftp7个人免费版官方下载,无需破解,免激活,下载即可使用
随机推荐
[physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code
Sqlilabs-1 (breakthrough record)
【物理应用】水下浮动风力涡轮机的尾流诱导动态模拟风场附matlab代码
MySQL常用的日期时间函数
弹框遮罩层「建议收藏」
Apk signature.Apk version information
frontiers出版社投稿记录(附状态变化)
CGLIb 创建代理
Advanced C language: pointer (2)
DirectX修复工具下载(exagear模拟器数据包在哪里)
一种分布式深度学习编程新范式:Global Tensor
Thesis reading (0) - alexnet of classification
[radar] radar signal online sorting based on kernel clustering with matlab code
Invest 145billion euros! EU 17 countries announce joint development of semiconductor technology
Will Qualcomm and MediaTek chips soon be sold, and will they surpass Huawei to become the first in China?
无代码开发平台通讯录导出入门教程
业界首创云原生安全检测双模型!安全狗重磅报告亮相数字中国建设峰会
[MySQL series] addition, deletion, modification and query of MySQL tables (Advanced)
定了!哪吒S全系产品将于7月31日上市发售
Leetcode 199. right view of binary tree