簡體   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