当前位置:网站首页>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 ');边栏推荐
- leetcode101. 对称二叉树
- [physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code
- Wheel 6: qserialport serial port data transceiver
- Subscript in swift
- Applet, JS, transfer object jump transfer parameter problem
- Several common methods of SQL optimization
- A simple neural network model based on MLP full connection layer
- General principles of software quality
- GCD implementation and arc, blocks, GCD usage examples
- Recurrent neural network (RNN)
猜你喜欢

Sdwebimage source code combs 5 author motivation, modifies directory, and changes inheritance relationship

Submission records of frontiers Publishing House (with status changes)
![[physical application] atmospheric absorption loss with matlab code](/img/72/e6ac23012a59ac48a37bcbb068890b.png)
[physical application] atmospheric absorption loss with matlab code

Applet, JS, transfer object jump transfer parameter problem

Thesis reading (2) - vggnet of classification

Xshell7, xftp7 personal free version official download, no need to crack, no activation, download and use

Thesis reading (3) - googlenet of classification

MySQL常用的日期时间函数

xshell7,xftp7个人免费版官方下载,无需破解,免激活,下载即可使用

Sqlilabs-1 (breakthrough record)
随机推荐
程序员成长第三十篇:识别真伪需求的神器
High quality subroutine 2 - high cohesion
[database]
No code development platform management background tutorial
The US FCC provided us $1.6 billion to support domestic operators to remove Huawei and ZTE equipment
解决serviceaccount用户认证挂载密文token文件失败导致pod使用anonymous用户问题
Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
Learning experience sharing 5: yolov5 dataset division and Yolo format conversion
GCD implementation and arc, blocks, GCD usage examples
Target detection notes -yolo
Cnpm installation steps
Shell script foundation - shell operation principle + variable and array definitions
【物理应用】大气吸收损耗附matlab代码
Xinhuazhang announced the completion of more than 200million a-round financing and the comprehensive layout of eda2.0 R & D
【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
WebApplicationType#deduceFromClasspath
In 2020, the top ten domestic IC design enterprises will be exposed! These five industrial challenges still need to be overcome!
投资500亿元!中芯京城正式注册成立!
How to automatically install homebrew in China (domestic address)
Reading of "robust and communication efficient federated learning from non-i.i.d. data"