且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JavaScript - “this"的所有者

更新时间:2023-02-21 07:40:40

如果你想让 this 属性保持一致,你应该绑定被调用的函数.

If you want to have the this property be consistent, you should bind the functions that are being called.

例如

setInterval(function() {/* code here */}.bind(this), 500)

这样,内部函数的this将与外部函数的this相同.

That way, the this of the inner function will be the same as that of the outer function.