有关函数返回另一个函数的几个帖子,如this post。但是,当返回的函数包含参数时会发生什么?使用参数返回函数的Javascript函数
我很困惑,如何调用返回函数,以及从哪里获取输入参数。这是从d3 collision feature.
例如采取一个例子,
force.on("tick", function(e) {
var q = d3.geom.quadtree(nodes), //q is a quadtree factory
i = 0, //counter variable
n = nodes.length; //number of nodes
while (++i < n)
q.visit(collide(nodes[i])); ///////// collide function called here /////////
); });
function collide(node) {
var r = node.radius + 25,
nx1 = node.x - r,
nx2 = node.x + r,
ny1 = node.y - r,
ny2 = node.y + r;
/////// How does the below function work?
/////// Arguments quad, x1, y1, x2, y2 are not passed,
////// but the code works
return function(quad, x1, y1, x2, y2) {
if (quad.point && (quad.point !== node)) {
//do something
}
return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
};
}
有一些很好的答案已经在这里,所以我不会认为,这被关闭。然而,你可能也想看看[这个答案在方法链和传递匿名函数作为参数](http://stackoverflow.com/a/21421101/3128209),它涵盖了许多相同的观点。 – AmeliaBR 2014-12-13 19:38:52