3 回答

TA貢獻1845條經驗 獲得超8個贊
如果您只想使用一個參數名稱,也許您打算對其進行解構:function(...add)
const not_hof = {};
// Cheating by using destructuring (...add)
// and not a HOF since accepts only Numbers as arguments
not_hof.add = function(...add) {
const makeAdd = add.reduce((prev, curr) => {
return prev + curr;
}, 0);
return makeAdd; // and still not a HOF since it returns a Number
};
console.log(not_hof.add(2, 3)); // 5
console.log(not_hof.add(9, 1, 10)); // 20
console.log(not_hof.add(1, 1, 1, 1, 1, 1)); // 6
PS:上面的函數也可以表示為:
not_hof.add = (...add) => add.reduce((prev, curr) => prev + curr, 0);
不是 HOF(高階函數)
雖然很多人會說上面是一個高階函數- 因為它返回Array.prototype.reduce
,它實際上不是:
在數學和計算機科學中,高階函數是至少執行以下一項操作的函數:
將一個或多個函數作為參數(即過程參數),
返回一個函數作為其結果。
但是,add
是不是一個程序函數的參數; IE:
過程 P(f):
?返回 f(2,3) * f(9,1)
并且它不返回函數;而一個號碼 返回的Array.prototype.reduce
。
1. HOF - 一個或多個函數作為參數
至少傳遞一個函數作為參數:
const helper = { // Helper functions
add(a, b) { return Number(a) + Number(b); },
};
const hof = {};
hof.add = function(fn, add1, add2) { // HOF since it takes a function as argument
return fn(add1, add2); // (Returns a Number)
};
// ...But it takes three arguments
console.log(hof.add(helper.add, 56, 5)); // 61
2. HOF - 返回一個函數
至少返回一個函數:
const hof = {};
hof.add = function(add) { // (Takes a Number argument)
function makeAdd(b) {
return add + b;
}
return makeAdd; // HOF since it returns a function
};
// But accepts a single argument at each call
console.log(hof.add(56)(5)); // 61
或喜歡
const hof = {};
hof.add = function(add1, add2) { // NOT LIKE YOUR EXAMPLE, 2 arguments are expected!
function makeAdd() {
return add1 + add2;
}
return makeAdd; // HOF since it returns a function
};
// ...The function had to be executed ()
console.log(hof.add(56, 5)()); // 61
但在這種情況下,它無法通過您的測試說明:
it('returns total of the two arguments', () => { // nope :( only one argument here...
帶閉合的 HOF
允許使用 a 多次調用該函數Function.prototype.toString(),以便在最后一次調用時返回字符串
const hof = {};
hof.add = function(add) {
let sum = add; // Store in local scope
const makeAdd = (b) => {
sum += b; // Access to lexical environment variables
return makeAdd; // Returns a function (self)
}
makeAdd.toString = () => sum;
return makeAdd; // HOF since we return a function
}
const result = hof.add(1)(2)(2)(56);
console.log(result) // (f) 61
console.log(typeof result); // function
console.log(result == 61) // true
console.log(result === 61) // false
// A legitimate test might break since the strict equality operator fails.
讓我們保持簡單。沒有關閉,沒有 HOF
如果不需要使用數組解構和 Array.prototype.reduce() 的第一個示例,那么只需堅持最簡單的函數聲明形式:
const not_hof = {
add: (a, b) => a + b,
sub: (a, b) => a - b,
// etc...
};
console.log( not_hof.add(56, 5) ); // 61

TA貢獻1788條經驗 獲得超4個贊
為什么要在返回的函數中創建一個函數,而不是最初創建正確的函數?我會做這樣的事情:
hof.add = function(a, b) {
return (a + b);
};

TA貢獻1820條經驗 獲得超9個贊
高階函數 (HOF) 只是接受函數作為參數或返回另一個函數的函數的一個奇特名稱。如果我更了解您,您正在嘗試創建一個可用于添加數字的加法器函數。使用您的示例 hof.add 是一個接收函數的 HOF,并返回另一個可用于將兩個數字相加的函數
hof.add = function(add) {
function addFunc(a, b) {
return add(a, b);
}
return addFunc;
}
function add(a, b){
return a + b
}
const addNumbers = hof.add(add);
addNumbers(3, 14)
//17 as answer
addNumbers can recieve any two numbers and it will add them together.
add 是一個將兩個數字相加的函數,它作為 hof.add 中的參數接收。hof.add 函數返回一個名為 addFunc 的函數,該函數又接收兩個要相加的參數。干杯
添加回答
舉報