结果的值为122,即使未指定乘数,它也会自动采用输入的值吗?我认为乘法运算符与x = x * y相同,因此需要首先指定x。

function mystery(input) {
  var secret = 4;
  input += 2;
  function mystery2(multiplier) {
    multiplier *= input;
    return secret * multiplier;
  }
  return mystery2;
}

function mystery3(param) {
  function mystery4(bonus) {
    return param(6) + bonus;
  }
  return mystery4;
}

var hidden = mystery(3);
var jumble = mystery3(hidden);
var result = jumble(2);

最佳答案

function mystery(input) {
  var secret = 4;
  input += 2;
  function mystery2(multiplier) {
     multiplier *= input;
     return secret * multiplier;
  }
  return mystery2;
}

function mystery3(param) {
   function mystery4(bonus) {
      return param(6) + bonus;
   }
  return mystery4;
}

var hidden = mystery(3);
  //input=3 +=2 = 5
  //hidden=mystery2

var jumble = mystery3(hidden);
 //param=hidden
 //jumble=mystery4

var result = jumble(2);
  //bonus=2
 //result=jumble(2)=mystery4(2)
 //result=param(6)+bonus
 //result=mystery2(6)+2
 //multiplier=6
 //result=input*multiplier*secret+2
 //result=5*6*4+2
 //result=?


您可以简化代码:

mystery=input=>multiplier=>(input+2)*multiplier*4;
mystery3=param=>bonus=>param(6)+bonus;
result=mystery3(mystery(3))(2);

关于javascript - 使用Ja赋值运算符关闭Javascript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43426814/

10-12 02:23