2014-12-13 89 views
0

有關函數返回另一個函數的幾個帖子,如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; 
     }; 
} 
+1

有一些很好的答案已經在這裏,所以我不會認爲,這被關閉。然而,你可能也想看看[這個答案在方法鏈和傳遞匿名函數作爲參數](http://stackoverflow.com/a/21421101/3128209),它涵蓋了許多相同的觀點。 – AmeliaBR 2014-12-13 19:38:52

回答

2

通過調用collide(nodes[i])返回的功能是爲visit功能的回調函數。回調函數在visit函數內執行。 visit函數提供了適當的參數。

但是我們在這裏不能自由選擇參數。應使用的參數已由visit函數的作者預定義(visit函數的參數記錄爲here)。

如:

//arguments of calc function are a function and two numbers 
 
    function calc(callback,x,y){ 
 
     callback(x,y); //the callback function is called with 2 parameters here. 
 
         //So the 1st argument to the calc function (callback) should also accept two arguments 
 
    } 
 
    
 
    calc(function(a,b){ //calling calc function with arg1 = "function to calculate sum", arg2 = 1, arg3 = 2 
 
     document.write(a+b); 
 
    },1,2);

0

你只需要使用這個像這樣:

collide(node)(quad, x1, y1, x2, y2); 
1

返回的功能很簡單,就是一個函數。所以四元組的參數在被調用時被定義。外功能只是作爲一個「包裝」爲nx值和ny

一個更簡單的,極人爲的例子:

function wrapName (personName) { 
    return function greet (greeting) { 
    return greeting + ' ' + personName; 
    }; 
} 

// greetBobWith is now the returned function, which accepts a `greeting` param 
// but still has a reference to `personName` 
var greetBobWith = wrapName('bob'); 

greetBobWith('Hello!'); // "Hello! Bob" 
greetBobWith('Happy Holidays'); // "Happy holidays, Bob" 
1

函數碰撞返回匿名函數。這個匿名函數不會立即調用。只有當返回的函數被保存爲一個變量並且該變量被調用時纔會被調用。

實施例:

//declare quad, x1, y1, x2, y2 here or in a parent scope 
var returnedFunction = collide(someNode); 
var returnedBool = returnedFunction(quad, x1, y1, x2, y2); 

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; 
     }; 
    } 
1

表達collide(nodes[i])的值是從collide返回的功能。它沒有在那裏被調用,但是該函數被傳遞給visit方法。

稍後visit將調用在collide中創建的函數,也就是當參數提供給函數時。類似於:

visit: function(collideFunc){ 

    // ... 

    var collided = collideFunc(quad, x1, y1, x2, y2); 

    // ... 

}