当前位置:网站首页>Typescript类方法this指针绑定
Typescript类方法this指针绑定
2022-07-28 21:36:00 【RemoteDev】
//类的this指针使用
class Log{
constructor() {
//为printX函数绑定当前类对象,解决调用this.print时找不到print方法
this.printX = this.printX.bind(this);
//通过箭头函数形式使this指针自动绑定方法
this.printY = ((y='y') => {
this.print(`this allow auto bind this,${y}`);
});
}
print(obj){
console.log(obj);
}
printX(x='a'){
this.print(`hi,${x}`);//使用了this指针
}
printY(x='a'){
this.print(`bind test :,${x}`);//使用了this指针
}
}
let l = new Log();
l.printX('HELLO');//可正常调用
//导出形式调用报错
const {printX,printY}=l;//从对象中导出
//printX调用的print方法找不到,this失效
//printX('WOW');//Cannot read properties of undefined (reading 'print')
//通过绑定this的形式解决,上面的构造函数中添加了函数绑定this对象
console.log('printX方法已绑定this指针,问题解决');
printX('WOW');
console.log('printY方法通过箭头函数自动绑定this指针,问题解决');
printY('===world===');
//通过代理器在获取函数方法时自动绑定this
function proxyAutoBindThis(obj){
let c = new WeakMap();//缓存
//代理器处理事件
let h = {
get (t,k){
let v = Reflect.get(t,k);
if (typeof v!=='function'){return v;}//不是函数不处理,直接返回
if (!c.has(v)){c.set(v,v.bind(t));}//不重复绑定
return c.get(v);//返回绑定了this的函数
}
};
let p = new Proxy(obj,h);//为传入的obj设置代理器
return p;//返回代理器对象
}
class A{
test(obj){
console.log(obj);
}
test1(obj=null){
this.test(`${obj}===>,test1`);//使用this指针
}
test2(obj=null){
this.test(`${obj}===>,test2`);//使用this指针
}
}
let a = new A();//实例化A对象a
let objBindThisByProxy = proxyAutoBindThis(a);//所有方法自动绑定this
let {test1,test2}=objBindThisByProxy;//从代理器导出
//类对象调用
a.test1('类对象调用');
a.test2('类对象调用');
//通过代理器调用
test1('通过代理器调用');
test2('通过代理器调用');
边栏推荐
- Performance optimized APK slimming
- 美国FCC提供16亿美元资助本国运营商移除华为和中兴设备
- PCA learning
- The applet vant webapp component is missing, and the referenced component reports an error
- cnpm安装步骤
- WebView whitelist
- Goer shares and Shanghai Taisi Weida growth cooperation agreement! Special SOC jointly promotes the development of TWS headphones
- GCD summary
- Leetcode 199. right view of binary tree
- Retrofit Usage Summary
猜你喜欢
Introduction to address book export without code development platform
[image segmentation] vein segmentation based on directional valley detection with matlab code
frontiers出版社投稿记录(附状态变化)
Console.log() console display... Solution
Basic concept of MySQL database and deployment of MySQL version 8.0 (I)
[filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code
The safety dog has been selected into many details of cloud security panorama 2.0
《Shortening passengers’ travel time A dynamic metro train scheduling approach using deep reinforcem》
c语言进阶篇:指针(三)
Several common methods of SQL optimization
随机推荐
frontiers出版社投稿记录(附状态变化)
Basic concept of MySQL database and deployment of MySQL version 8.0 (I)
Use FFT, matrix multiplication and conv2d to calculate convolution based on pytorch
Thesis reading (1) - zfnet of classification
芯华章宣布完成超2亿A轮融资,全面布局EDA2.0研发
【C语言】三子棋小游戏实现
The simple neural network model based on full connection layer MLP is changed to the model based on CNN convolutional neural network
[copy] Internet terms, abbreviations, abbreviations
Servlet的使用手把手教学(一)
Learning experience sharing 3: yolov5 training data set path index
Introduction to original code, inverse code and complement code
Bullet frame mask layer "recommended collection"
软件测试工具fiddler postman jmeter charlse核心功能总结
【MongoDB】MongoDB数据库的基础使用,特殊情况以及Mongoose的安装和创建流程(含有Mongoose固定版本安装)
Leetcode101. Symmetric binary tree
There are four ways for Nacos to configure hot updates and multiple ways to read project configuration files, @value, @refreshscope, @nacosconfigurationproperties
The US FCC provided us $1.6 billion to support domestic operators to remove Huawei and ZTE equipment
Performance optimized APK slimming
RouYi-Cloud平台 ---项目的启动、登录功能是怎么实现的、怎么样创建新模块
MySQL foundation - advanced functions