const numbers = [65, 44, 12, 4];
const tPass = 'hello';
numbers.forEach(myFunction)
function myFunction(item, index) {
console.log(item);
}
我如何將 tPass 值傳遞給 myFunction,并顯示例如 console.log(tPass item);
uj5u.com熱心網友回復:
你可以使用系結:
const numbers = [65, 44, 12, 4];
const tPass = 'hello';
numbers.forEach(myFunction.bind(null, tPass))
function myFunction(tPass, value, index) {
console.log(tPass, value, index);
}
uj5u.com熱心網友回復:
forEach 方法框架可以具有您之前撰寫的初始 (item, index) 引數。因此,為了修改它,您必須確保始終回傳具有相同引數結構的函式。
含義:您宣告了一個高階函式,我們將其稱為 myHigherFunction。myHigherFunction 接受“tpas”并在其中應用 myFunction 的邏輯,并在最后回傳它。最后,您將 tPass 傳遞給 myHigherFunction。
于是代碼變成了這樣:
const numbers = [65, 44, 12, 4];
const tPass = 'hello';
numbers.forEach(myHigherFunction(tPass))
function myHigherFunction(tPass){
function myFunction(item, index) {
console.log(tPass item);
}
return myFunction
}
請記住始終回傳具有所需骨架(初始 2 個引數)的函式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/388676.html
標籤:javascript html
下一篇:將按鈕添加到最后一列
