繁体   English   中英

arguments是如何传入回调函数的?

[英]How arguments are passed in callback functions?

我已经开始学习 JavaScript 以进行 web 开发,我目前在回调 function 中陷入困境。 问题是我无法理解 arguments 是如何在 JavaScript 中传递的。

代码:

const arr = [1, 2, 3, 4, 5, 6, 7, 8];
function myfunc(value){ //i had set a parameter 'value'
  console.log(value); // i had printed the 'value'
}
arr.forEach(myfunc); // i had not passed any argument in myfunc

我真的很困惑myfunc (value)如何从 forEach function 或任何函数中获取“值”参数:

const numbers1 = [45, 4, 9, 16, 25];
function myFunction(value) { //myFunction has parameter 'value'
  return value * 2;
}
const numbers2 = numbers1.map(myFunction); /* here, how value arguments are passed to 
myFunction? */

它们被传入是因为forEach有一些代码可以传入它们。forEach 的实现看起来像这样:

forEach(callback) {
  // `this` is the array we're looping over
  for (let i = 0; i < this.length; i++) {
    callback(this[i], i, this);
  }
}

所以forEach处理循环遍历数组的逻辑,然后对于数组中的每个元素,它将调用您的 function 并传入三个参数(值、它的索引和整个数组)。 您只需编写一个 function 以任何您需要的方式使用这些值。 如果您不需要使用所有 3 个参数,您可以简单地忽略所需参数右侧的任何 arguments。

javascript Array上的功能扩展需要function作为参数。 那 function 可以是:

  • 一个名为 function
function doSomething() {} // This is a named function
  • 匿名 function
// This is an anonymous function because we didnt give a name to it
[1,2,3].forEach(function (value) { console.log(value) })
  • 粗箭头 function(lambda 表达式)。
// It's called fat arrow because well, the arrow is fat
[1,2,3].forEach((value) => console.log('hey', value))

Array上的功能扩展的实现总是传递三个 arguments:值、索引和 function 应用到的数组

The way function arguments work in JS is that if you pass more than the required arguments to a function JS will just drop the rest, and if you pass more than the ones needed those will have a value of undefined unless you have specified a default value对于那些

const array = [1,2,3]

// I am just getting the value and the name "value" could be any name
array.forEach((value) => console.log(value)) 
// here my fat-arrow function takes two parameters, since forEach passes three parameters we're good to go
array.forEach((value, index) => console.log(value, 'at', index))
// Here we're using all arguments without dropping any
array.forEach((value, index, array) => console.log(value, index, array)) 
// that is because  the forEach predicate only passes three arguments and hence the last is undefined
array.forEach((value, index, array, fourth) => console.log('here', fourth, 'is always undefined')) 

这对 OP 来说是有意义的,可能是:

const numbers1 = [45, 4, 9, 16, 25];
numbers1.forEach(e => console.log(e));

所以这可能也是有意义的:

const numbers1 = [45, 4, 9, 16, 25];
const someFunction = e => console.log(e);
numbers1.forEach(someFunction);

如果是这样,那么这也是有道理的:

function someFunction(e) {
  console.log(e));
}
numbers1.forEach(someFunction);

使用arr.forEach(myfunc)numbers1.map(myFunction) ,您将对 function 的引用传递给标准函数mapforEach 然后,这两个标准数组函数都在内部使用您的引用,并始终使用相同的参数调用您的 function。

因此,例如对于forEach ,在每个循环中,您的 function 被称为myfunc(element, index, array) 参数由forEach function 定义。 您还可以将这样的 function 内联定义为arr.forEach(function (element, index, array) {...}但它基本上只是做同样的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM