我知道我下面的代码是错误的,但很明显,我遗漏了一些信息。我必须写出更复杂的高阶函数,但是如果我不能真正理解简单的函数,我就没有机会了。
hof.add应该返回传递的两个参数的总数。很简单..。但鉴于我要创建这个高阶函数,必须使用闭包。
hof.add = function(add) {
function makeAdd(a, b) {
return add(a + b);
}
return makeAdd;
};it('returns total of the two arguments', () => {
expect(hof.add(56, 5)).to.be.equal(56 + 5);
expect(hof.add(91, -71)).to.be.equal(91 + -71);
});(给出的框架可在此基础上建立)
hof.add = function() { };发布于 2019-09-05 21:24:58
为什么要在返回的函数中创建一个函数,而不是最初创建正确的函数?我会这样做:
hof.add = function(a, b) {
return (a + b);
};发布于 2019-09-05 21:52:53
高阶函数( Higher,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的函数,该函数接收两个参数以将其相加在一起。干杯
发布于 2019-09-06 01:37:40
如果您只想使用一个参数名称,那么您可能是指destructure it: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是,而不是,它是一个过程函数参数;即:
程序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
允许使用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
https://stackoverflow.com/questions/57813120
复制相似问题