回调没有必要参数 - 为什么? [英] Callback that didn't need parameters - why?

查看:147
本文介绍了回调没有必要参数 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在这2个部分的问题,并设法解决它,但是我对code实际上是如何工作的一些问题 - 回调函数的具体性质。下面是两部分摆出来,连同我的解决方案这两个问题。我能够比较容易地解决了第一部分,但第二次是更具挑战我。

\r
\r
问题

//第一部分\r
\r
VAR合并=功能(数组1,数组2,回调){\r
  //你的code在这里。\r
}\r
\r
变种X =合并([1,2,3,4],[5,6,7,8],功能(A,B){\r
  返回A + B;\r
});\r
\r
// X应该现在等于[6,8,10,12]。\r
\r
\r
//我的解决方案:\r
VAR合并=功能(数组1,数组2,回调){\r
  变种newArray = [];\r
  \r
  对于(VAR I = 0; I< array1.length;我++){\r
    newArray [I] =回调(数组1 [I]中,数组2 [I]);\r
  }\r
  返回newArray;\r
}\r
\r
变种X =合并([1,2,3,4],[5,6,7,8],功能(A,B){\r
  返回A + B;\r
}); // x等于[6,8,10,12]。

\r

\r
\r

现在在这里,我是能够解决问题的第二部分,但如果我有麻烦是通过code一步一步走专。我的问题是 - 为什么我不需要给回调参数的合并功能的任何参数?当我试图提供参数x成为NaN的。

欧氏距离=开方((X2-X1)^ 2 +(Y2-Y1)^ 2))。

\r
\r

VAR欧几里得=功能(coords1,coords2){\r
  //你的code在这里。\r
  //你不应该使用任何循环和应\r
  //改用原来的合并功能。\r
}\r
\r
VAR X =欧几里德([1.2,3.67],[2.0,4.4]);\r
\r
//位x现在应该约等于1.08。\r
\r
\r
//我的解决方案:\r
VAR欧几里得=功能(coords1,coords2){\r
 // //为什么()函数如下不需要参数?\r
  VAR合并=合并(coords1,coords2,函数(){\r
    返回(coords1 [0] - coords2 [0])*(coords1 [0] - coords2 [0])+\r
    (coords1 [1] - coords2 [1])*(coords1 [1] - coords2 [1]);\r
  });\r
  \r
  返回的Math.sqrt(合并[0]);\r
};\r
\r
VAR X =欧几里德([1.2,3.67],[2.0,4.4]); //点​​¯x不等于约1.08。

\r

\r
\r

我是一个新手到编程,所以我意识到这可能是一个相当琐碎的问题。但走在我通过怎样的回调实际上是在此解决方案的工作任何帮助将是非常美联社preciated。谢谢!


解决方案

  

为什么我不需要给回调参数合并功能的任何参数?


假设:

  VAR欧几里得=功能(coords1,coords2){  //为什么()函数如下不需要参数?
  VAR合并=合并(coords1,coords2,函数(){
    返回(coords1 [0] - coords2 [0])*(coords1 [0] - coords2 [0])+
    (coords1 [1] - coords2 [1])*(coords1 [1] - coords2 [1]);
  });  返回的Math.sqrt(合并[0]);
};

传递到的合并的函数(回调)函数有一个封闭的外部范围的变量。其中的 coords1 的和的 coords2 的中的欧几里得的使他们局部变量,所以他们并不需要被传递给回调正式的参数列表。

如果该函数的范围之外创建的欧几里得的,那么你需要通过他们(或引用他们的一些其他的方式)。

顺便说一句,我preFER函数声明为前pressions的分配,例如。

 函数欧几里得(coords1,coords2){
  // code在这里
}

,因为它使得它更明显的函数的名称是什么。试想一下:

 无功富=功能弗雷德(){
  //这里富或弗雷德
}; // foo的只有在这里

So, I have been working on this 2-part problem and managed to solve it, however I have some questions on how the code actually works - specifically the nature of the callback function. Below are the 2 parts of the problem laid out, along with my solution to both. I was able to solve the first part rather easily, but the second was much more challenging for me.

// FIRST PART OF PROBLEM

var merge = function(array1, array2, callback){  
  //your code here.
}

var x = merge([1, 2, 3, 4], [5, 6, 7, 8], function(a, b){  
  return a + b;
});

//x should now equal [6, 8, 10, 12].


// MY SOLUTION: 
var merge = function(array1, array2, callback){
  var newArray = [];
  
  for (var i = 0; i < array1.length; i++) {
    newArray[i] = callback(array1[i], array2[i]);
  }
  return newArray;
}

var x = merge([1, 2, 3, 4], [5, 6, 7, 8], function(a, b){  
  return a + b;
}); // x equals [6, 8, 10, 12].

Now here is the second part of the problem that I was able to solve, but where I am having trouble is in specifically walking through the code step by step. My question is - why did I not need to give the callback parameter to the merge function any parameters? When I did attempt to provide parameter, x became NaN.

euclidian distance = sqrt((x2-x1)^2 + (y2-y1)^2))."

var euclid = function(coords1, coords2){  
  //Your code here.
  //You should not use any loops and should
  //instead use your original merge function.
}

var x = euclid([1.2, 3.67], [2.0, 4.4]);

//x should now equal approximately 1.08.


// My solution:
var euclid = function(coords1, coords2){
 // // why does function() below require no parameters?
  var merged = merge(coords1, coords2, function() {
    return (coords1[0] - coords2[0]) * (coords1[0] - coords2[0]) +
    (coords1[1] - coords2[1]) * (coords1[1] - coords2[1]);
  });
  
  return Math.sqrt(merged[0]);
};

var x = euclid([1.2, 3.67], [2.0, 4.4]); //x does equal approximately 1.08.

I'm a newbie to programming, so I realize this is probably a rather trivial question. But any help with walking me through how the callback is actually working in this solution would be much appreciated. Thanks!

解决方案

why did I not need to give the callback parameter to the merge function any parameters?

Given:

var euclid = function(coords1, coords2){

  // why does function() below require no parameters?
  var merged = merge(coords1, coords2, function() {
    return (coords1[0] - coords2[0]) * (coords1[0] - coords2[0]) +
    (coords1[1] - coords2[1]) * (coords1[1] - coords2[1]);
  });

  return Math.sqrt(merged[0]);
};

The (callback) function passed to the merge function has a closure to the variables of its outer scope. Including coords1 and coords2 in the formal parameter list of Euclid makes them local variables, so they don't need to be passed to the callback.

If the function was created outside the scope of Euclid, then you'd need to pass them (or reference them some other way).

Incidentally, I prefer function declarations to assignment of expressions, e.g.

function euclid(coords1, coords2) {
  // code here
}

as it makes it more obvious what the function's name is. Consider:

var foo = function fred() {
  // foo or fred in here
};

 // only foo out here

这篇关于回调没有必要参数 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆