有人可以解释这个调用函数在这个方法中是如何工作的吗

can someone explain how this call function works in this method

第 31 行在这里 --> https://github.com/benib/aurelia-hammer/blob/master/src/hammer-press.js

this.callback.call(null, { hammerEvent: event });

谁能解释一下这个 hammerEvent 对象是如何映射回调用 aurelia 视图模型中的 $event 对象的?

在Javascript中函数可以通过call方法调用,原因函数可以是变量。

var p = function(x)
{
    console.log('calling via Call method. arguments: ' + x);
}

现在如果我想调用这个函数,调用它的一种方法是使用如下调用方法。

p.call(null,'test')

有关调用函数的更多详细信息here

我认为连接对象和地图是在 hammer js inputHandlercomputeInputData

中完成的

https://hammerjs.github.io/dist/hammer.js

希望对您有所帮助!

Javascript提供三种函数调用方式

  1. 简单的方法,

假设你有一个函数

function foo() {

}

你只需用括号调用它,如 foo()

  1. 通过使用Function.prototypecall方法

好吧,再考虑一下foo函数

但这一次你需要换个角度看,javascript中的每个函数也是一个对象,每个对象都有一个原型,原型可以有更多的函数和属性。因此,当你定义一个像 foo 这样的函数时,它也会成为 Function 的对象,并为你提供两个额外的方法来调用该函数,其中一个是 call ,它期望第一个参数是上下文或 this 然后以逗号分隔的要传递给函数的参数列表。例如,您有一个具有属性 firstNamelastName 的用户对象,并且您希望拥有可以向该用户说些什么的函数,您可以像这样定义函数

function saySomething(something) {
 return "Hey " + this.firstName + " " + this.lastName} + " I wanted to say " + something
}
  1. 通过使用Function.prototypeapply方法

call 非常相似,除了您将传递一个数组而不是用逗号分隔的参数。

这里有更多内容https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function

Can someone explain how this hammerEvent object gets mapped back to the $event object in the calling aurelia view model?

到达那里有一些事情发生了。

假设您在视图中像这样使用它:

<div hammer-tap.call="handleTap($event)">
</div>
  1. 编译 hammer-tap 属性时,aurelia-templating-binding 将通过一系列约定到 map the binding attributes 以得到正确的表达式。

  2. 根据 .call 的约定,如您在 syntax-interpreter 中所见,它被转换为 CallExpression(位于 aurelia-binding

  3. 当编译完成并在组件上调用 bind() 时,此 CallExpression sets a delegate on the target 将使用它从中获得的任何值调用 callSource目标(在你的例子中:{ hammerEvent: event }

现在完全涵盖这一点有点棘手,因为 Aurelia 如何解决所有这些问题的深度和递归,但在 aurelia-templating) 中的 ListenerExpression is created (createBinding is invoked by ViewFactory 行的某处,它本质上添加了一个事件有问题的元素的听众。

  1. ListenerExpression 将使用正确的事件调用 hammer 的 handleSwipe 方法。

我相信在这个特定场景中实际上不会发生这种情况,因为作者似乎将 hammerjsown event handler.on.off 一起使用。所以它实际上是 hammerjs 将该事件从常规事件侦听器传递给 handleSwipe

  1. 回到CallExpressioncallSource最终会调用绑定到.call的函数,并传入从ListenerExpression接收到的$event (或 hammerjs 的处理程序)较早。

希望这对您有所帮助。但是正如您所看到的,它非常复杂:)