当前位置:网站首页>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('通过代理器调用');边栏推荐
- Several common methods of SQL optimization
- 即将获售高通、联发科芯片,荣耀要超越华为做国内第一?
- GCD implementation and arc, blocks, GCD usage examples
- Applet Download Preview PDF, document cannot open solution
- MySQL Basics - Introduction and basic instructions
- Improvement 11 of yolov5: replace backbone network C3 with lightweight network mobilenetv3
- Servlet的使用手把手教学(一)
- NPM run dev, automatically open the browser after running the project
- 无代码开发平台通讯录导出入门教程
- A new paradigm of distributed deep learning programming: Global tensor
猜你喜欢

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

【C语言】三子棋小游戏实现

Retrofit Usage Summary

Advanced C language: pointer (3)
![[physical application] atmospheric absorption loss with matlab code](/img/72/e6ac23012a59ac48a37bcbb068890b.png)
[physical application] atmospheric absorption loss with matlab code

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

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

Target detection notes - overview and common data sets

Advanced C language: pointer (2)

The safety dog has been selected into many details of cloud security panorama 2.0
随机推荐
CGLIb 创建代理
Will Qualcomm and MediaTek chips soon be sold, and will they surpass Huawei to become the first in China?
Bullet frame mask layer "recommended collection"
Target detection notes - overview and common data sets
一种分布式深度学习编程新范式:Global Tensor
【雷达】基于核聚类实现雷达信号在线分选附matlab代码
Summary of common formula notes for solving problems in Higher Mathematics
Advanced C language: pointer (3)
In 2020, the top ten domestic IC design enterprises will be exposed! These five industrial challenges still need to be overcome!
Anr questions often asked in Android interviews
Introduction to original code, inverse code and complement code
美国FCC提供16亿美元资助本国运营商移除华为和中兴设备
Console.log() console display... Solution
6 个超级良心的开源教程!
Xinhuazhang announced the completion of more than 200million a-round financing and the comprehensive layout of eda2.0 R & D
(important) first knowledge of C language -- function
投资500亿元!中芯京城正式注册成立!
One of the experience of solving problems with code error reporting: CUDA does not match pytorch
The Gerrit local code is associated to the remote warehouse
No code development platform management background tutorial