3 回答

TA貢獻1845條經(jīng)驗 獲得超8個贊
如果您只想使用一個參數(shù)名稱,也許您打算對其進行解構(gòu):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:上面的函數(shù)也可以表示為:
not_hof.add = (...add) => add.reduce((prev, curr) => prev + curr, 0);
不是 HOF(高階函數(shù))
雖然很多人會說上面是一個高階函數(shù)- 因為它返回Array.prototype.reduce
,它實際上不是:
在數(shù)學(xué)和計算機科學(xué)中,高階函數(shù)是至少執(zhí)行以下一項操作的函數(shù):
將一個或多個函數(shù)作為參數(shù)(即過程參數(shù)),
返回一個函數(shù)作為其結(jié)果。
但是,add
是不是一個程序函數(shù)的參數(shù); IE:
過程 P(f):
?返回 f(2,3) * f(9,1)
并且它不返回函數(shù);而一個號碼 返回的Array.prototype.reduce
。
1. HOF - 一個或多個函數(shù)作為參數(shù)
至少傳遞一個函數(shù)作為參數(shù):
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 - 返回一個函數(shù)
至少返回一個函數(shù):
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 多次調(diào)用該函數(shù)Function.prototype.toString(),以便在最后一次調(diào)用時返回字符串
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.
讓我們保持簡單。沒有關(guān)閉,沒有 HOF
如果不需要使用數(shù)組解構(gòu)和 Array.prototype.reduce() 的第一個示例,那么只需堅持最簡單的函數(shù)聲明形式:
const not_hof = {
add: (a, b) => a + b,
sub: (a, b) => a - b,
// etc...
};
console.log( not_hof.add(56, 5) ); // 61

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

TA貢獻1820條經(jīng)驗 獲得超9個贊
高階函數(shù) (HOF) 只是接受函數(shù)作為參數(shù)或返回另一個函數(shù)的函數(shù)的一個奇特名稱。如果我更了解您,您正在嘗試創(chuàng)建一個可用于添加數(shù)字的加法器函數(shù)。使用您的示例 hof.add 是一個接收函數(shù)的 HOF,并返回另一個可用于將兩個數(shù)字相加的函數(shù)
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 是一個將兩個數(shù)字相加的函數(shù),它作為 hof.add 中的參數(shù)接收。hof.add 函數(shù)返回一個名為 addFunc 的函數(shù),該函數(shù)又接收兩個要相加的參數(shù)。干杯
添加回答
舉報