【问题标题】:ES6 functions help - functions returning functions [closed]ES6函数帮助-返回函数的函数[关闭]
【发布时间】:2017-08-31 11:54:49
【问题描述】:

我对 ES6 很陌生。

尝试通过一些测试来学习。

请帮助我了解通过测试的实现方式。

// dependencies:
const expect = require('chai').expect;

 // implement this:
 function b(x){
   // return "b"+ x;
   //  return (x) =>  "bo" + x;
    
 }
// unit tests:
describe("implement function b", function() {
  it("SHOULD work for the following cases", function() {
    console.log(b()("m"));
    expect(b("m")).to.equal("bm");
    expect(b()("m")).to.equal("bom");
    expect(b()()("m")).to.equal("boom");
    expect(b()()()("m")).to.equal("booom");
    expect(b()()()()("t")).to.equal("boooot");
  });
});

【问题讨论】:

  • 我们不鼓励那些简单地断章取义地陈述问题的帖子,并希望社区能够解决它。假设您尝试自己解决并陷入困境,如果您写下您的想法和您无法弄清楚的内容,这可能会有所帮助。它肯定会为您的帖子带来更多答案。在此之前,该问题将被投票关闭/否决。

标签: javascript ecmascript-6


【解决方案1】:

这是可能的,但有点奇怪,我永远不会在现实生活中做这样的事情。

通常,返回函数的函数称为“二阶”函数。返回函数的函数是“三阶”函数。您要做的是编写一个根据参数具有不同顺序的函数,这确实使阅读和维护感到困惑。

话虽如此,javascript 对返回类型并不挑剔,所以你可以做到。这是我要使用的代码(使用 ES6 默认变量和递归)

function b(lastLetter, prefix = "b") {
  if (lastLetter) {
    //if we're ending the chain, return everything so far with the last letter on the end
    return prefix + lastLetter;
  }
  //if not, then return version of this function with a slightly longer prefix
  return lastLetter => b(lastLetter, prefix + "o");
}

console.log( b("m") );
console.log( b()("m") );
console.log( b()()("m") );
console.log( b()()()()()()()("t") );

【讨论】:

  • 我考虑过这一点,但不喜欢那样使用第二个参数,而是使用了闭包。做这项工作。
【解决方案2】:

您可以使用闭包和命名函数表达式,请参阅 cmets。我不喜欢重复的线条,但无法避免这种模式。

function b(x) {

  // On the first call, setup prefix 
  var prefix = 'b';

  // End early if x provided on first call
  if (x) return prefix + x;

  // Otherwise, return a function that can be chained
  return function foo(x){
    prefix += 'o';
    if (x) return prefix + x;
    return foo;
  } 
}

console.log(b('m'));
console.log(b()('m'));
console.log(b()()('m'));
console.log(b()()()('m'));
console.log(b()()()()('t'));

这种模式的问题是:

  1. 如果在最后一次调用中没有提供字母,则返回一个函数。特定呼叫无法知道它是最后一次。
  2. 如果在提供字母后进行调用,它将尝试调用字符串,这将引发错误。同样,一旦用户尝试提供字母,就无法停止呼叫。

【讨论】:

  • 是的,我同意,这也是我强烈反对这种方法的原因之一!不过它确实通过了测试。
  • @DuncanThacker 您的解决方案有同样的问题:P 甚至比这个问题更多。我想知道您为什么强烈反对这种方法?因为这种方法比你的方法好。
  • 抱歉不清楚 - 问题是试图完成一些我在现实生活中永远不会尝试做的事情。你的解决方案(和我的)正确回答了这个问题,我并没有试图把你的答案记下来,
【解决方案3】:

显然,b 必须返回一个函数,如果没有参数传递给它。这个函数的行为方式相同:如果没有参数传递给它,它会返回自己。此外,我们必须跟踪我们的函数被调用了多少次。

下面的解决方案创建一个内部函数,如果它的参数是假的,它会增加计数,否则它会创建一个由"b""o" 重复次数指定的字符串和参数的值:

const b = v => {
  let n = 0; // this is our counter
  const f = e => {
    if (e !== undefined) {
      // an argument was passed, return the correct value
      return 'b' + 'o'.repeat(n) + e;
    }
    // no argument was passed, increment the counter and return the function
    n += 1;
    return f;
  };
  // call the function the first time with the initial value
  return f(v);
};

console.log(b('m'));
console.log(b()('m'));
console.log(b()()('m'));
console.log(b()()()('m'));
console.log(b()()()('t'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 2019-11-25
    • 2013-04-18
    • 1970-01-01
    • 2011-03-13
    • 2010-11-20
    相关资源
    最近更新 更多