【发布时间】:2019-12-15 05:12:11
【问题描述】:
是否可以在 javascript 中分离函数,同时允许它们保留对其上下文的访问权限?
例如,假设我们有一个名为 vc 的 ViewportScroller 实例。我们可以通过调用获取当前滚动位置:
vc.getScrollPosition()
是否可以这样分离:
cont scrollPosition = vc.getScrollPosition
在确保它仍然可以工作并且能够访问它需要工作的所有内容的同时,我们是否总是需要像这样包装vc:
const scrollPostion = ()=>vc.getScrollPosition()
【问题讨论】:
-
let scrollPostion = vc.getScrollPosition.bind(vc)。见How does the “this” keyword work? 和What is the use of the JavaScript 'bind' method?
标签: javascript node.js typescript