分配合适的“这个”; setTimeout(f, t) 中的值
有没有办法调用 setTimeout(f, t) 以便为 f 分配一个自己选择的“this”?
Is there a way to call setTimeout(f, t) such that f is assigned a 'this' of one's choice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是一个 Javascript 问题。我不知道您可以使用本机
setTimeout
来做到这一点,并且(显然像您一样)我找不到任何表明您可以这样做的文档。但你当然可以推出一个这样做的:编辑:可能你只需使用
Function.bind
来重新绑定f
中的this
。根据喜好,您甚至可以在相关范围内重新定义
setTimeout
;不过,我可能不会,因为这可能会打破读者的假设。I take it this is a Javascript question. I don't know that you can do that using the native
setTimeout
, and (apparently like you) I couldn't find any documentation indicating that you can. But you can certainly roll one that does:EDIT: possibly you would just use
Function.bind
to rebindthis
inf
.Depending on taste you might even redefine
setTimeout
in the relevant scope; I probably wouldn't, though, as it may break readers' assumptions.