更好地理解 javascript 中的回调函数

发布于 2024-09-27 12:26:02 字数 352 浏览 5 评论 0原文

我正在书上读这个函数,但我不明白匿名函数是如何实现的 是通过了口渴三论吗?

这是我的理解

function multiplyByTwo(a, b, c, callback) {
  var i, ar = [];
  for(i = 0; i < 3; i++) {
    ar[i] = callback(arguments[i] * 2);
  }
  return ar;
}

,但不是这个

myarr = multiplyByTwo(1, 2, 3, function(a){return a + 1});

,谢谢,理查德

I am reading this function in a book, and I don't understand how the anonymous function
is passed the thirst three arguments?

This is what I understand

function multiplyByTwo(a, b, c, callback) {
  var i, ar = [];
  for(i = 0; i < 3; i++) {
    ar[i] = callback(arguments[i] * 2);
  }
  return ar;
}

but not this

myarr = multiplyByTwo(1, 2, 3, function(a){return a + 1});

thanks, Richard

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

姜生凉生 2024-10-04 12:26:02

一些详细说明 multiplyByTwo 函数约定的注释在这里会很有帮助:

// multiplyByTwo
// num num num (num -> x) -> x[]
function multiplyByTwo(a, b, c, callback) {
  ...

这意味着该函数接受四个参数,并返回一个 x 数组。 x 可以是任何类型。前三个参数是数字。函数的第四个参数实际上是函数本身。该函数接受一个数字,并返回一个x。同样,x 可以是任何类型,但它在整个合约中都是相同的类型。

multiplyByTwo 函数内,为每个其他参数调用一次回调函数。每个调用的结果都会添加到一个数组中,即 multiplyByTwo 的返回值。

arguments 变量是 javascript 中的一个特殊变量,它在函数作用域内以数组形式提供对该函数的所有参数的访问。

回调函数绑定到 multiplyByTwo 函数范围内的名称 callback。然后可以像任何其他函数一样调用它,例如通过附加括号和参数。然后可以通过该名称引用您在调用 multiplyByTwo 时提供的匿名函数。

因此,在给出的示例中,使用四个参数调用 multiplyByTwo123 和一个匿名函数。在 multiplyByTwo 函数的范围内,这些参数绑定到变量 abc 和分别是回调。这些参数也可以通过特殊的 arguments 数组引用。在示例中,这两种方法都用于引用参数。前三个参数使用循环内的 arguments 数组进行引用,第四个参数使用其名称进行引用,如 callback(...) 中所示。

Some comments detailing the contract of the multiplyByTwo function would be helpful here:

// multiplyByTwo
// num num num (num -> x) -> x[]
function multiplyByTwo(a, b, c, callback) {
  ...

This means that the function takes four arguments, and returns an array of x. x can be any type. The first three arguments are numbers. The fourth argument to the function is actually a function itself. This function takes a number, and returns an x. Again, x can be any type, but it is the same type throughout the contract.

Inside the multiplyByTwo function, the callback function is called once for each of the other arguments. The result of each of these calls is added to an array, which is the return value of multiplyByTwo.

The arguments variable is a special variable in javascript, which, inside the scope of a function, gives access to all of the arguments to that function, as an array.

The callback function is bound to the name callback in the scope of the multiplyByTwo function. It can then be called as any other function, such as by appending parentheses and arguments. The anonymous function that you gave when you called multiplyByTwo can then be referenced by that name.

So, in the example given, multiplyByTwo is called with four arguments: 1, 2, 3, and an anonymous function. Inside the scope of the multiplyByTwo function, these arguments are bound to the variables a, b, c, and callback, respectively. These arguments can also be referenced through the special arguments array. In the example, both methods are used to reference the arguments. The first three are referenced using the arguments array inside a loop, and the fourth argument is referenced using its name, as in callback(...).

当爱已成负担 2024-10-04 12:26:02

匿名函数在循环中被调用 3 次,每次传递不同的参数。第一次传入 arguments[0],即 a(而 arguments[1] 将为 b > 等)。 arguments 是当前执行函数的属性,指传递到函数中的参数。在 JavaScript 中,您不必为参数命名,也不必通过名称访问它们,甚至不必传入所有命名参数。

The anonymous function is called 3 times in the loop, each time passing in a different argument. The first time it passes in arguments[0], which is a (and arguments[1] would be b, etc.). arguments is a property of the currently executing function and refers to the parameters passed into the function. In JavaScript, you don't have to name your parameters, or access them by their names, or even pass in all of the named parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文