当前位置:网站首页>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 ');边栏推荐
- DirectX修复工具下载(exagear模拟器数据包在哪里)
- How to automatically install homebrew in China (domestic address)
- Performance optimized APK slimming
- Typescript类方法this指针绑定
- Sqlilabs-3 (entry notes)
- The applet vant webapp component is missing, and the referenced component reports an error
- Typescript防止基类被实例化
- Target detection notes - overview and common data sets
- 解决serviceaccount用户认证挂载密文token文件失败导致pod使用anonymous用户问题
- No code development platform management background tutorial
猜你喜欢

Thesis reading (2) - vggnet of classification

【物理应用】水下浮动风力涡轮机的尾流诱导动态模拟风场附matlab代码

Advanced C language: pointer (3)

After reading MySQL database advanced practice (SQL xiaoxuzhu)

NVM use... Exit status garbled

这个胶水有多强呢?

【雷达】基于核聚类实现雷达信号在线分选附matlab代码

Target detection notes fast r-cnn

Sqlilabs-3 (entry notes)

Thesis reading (3) - googlenet of classification
随机推荐
软件测试面试笔试题及答案(软件测试题库)
Target detection notes fast r-cnn
Summary of core functions of software testing tool Fiddler postman JMeter charlse
DirectX修复工具下载(exagear模拟器数据包在哪里)
GCD implementation and arc, blocks, GCD usage examples
The Gerrit local code is associated to the remote warehouse
《Shortening passengers’ travel time A dynamic metro train scheduling approach using deep reinforcem》
Rouyi cloud platform - how to realize the launch and login functions of the project and how to create new modules
c语言进阶篇:指针(二)
Sqlilabs-1 (breakthrough record)
Hands on Teaching of servlet use (1)
2020年国内十大IC设计企业曝光!这五大产业挑战仍有待突破!
1.8tft color screen test code (stm32f407ve)
Thesis reading (1) - zfnet of classification
【MongoDB】MongoDB数据库的基础使用,特殊情况以及Mongoose的安装和创建流程(含有Mongoose固定版本安装)
Cglib create proxy
Pgbench benchmark PostgreSQL
Thesis reading (2) - vggnet of classification
【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
芯华章宣布完成超2亿A轮融资,全面布局EDA2.0研发