There are several posts around regarding a function returning another function, like this post here. However, what happens when the returned function contains parameters?
I'm confused as to how the return function is called, and where it gets its input arguments from. This is an example taken from a d3 collision feature.
For example,
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;
};
}