【问题标题】:Extracting Arguments from a Function Passed as a Parameter to Another Function in JavaScript从作为参数传递给 JavaScript 中另一个函数的函数中提取参数
【发布时间】:2018-10-14 05:45:49
【问题描述】:

我是 JavaScript 新手。我有一个小程序,其中一个函数将另一个函数作为参数。我正在尝试提取/访问作为参数传递的函数的参数。这是一个例子:

function test(precondition, postcondition, func)   {
   // Extract arguments of func which in this case should be 5 and 6
   // This is required to check whether isNumber(5) and isNumber(6)
   // both return true, so that precondition is met
 }

var add = test((isNumber, isNumber), isNumber,
            function add(x, y) {return x+y; });

console.log(add (5, 6));

isNumber 是一个函数,如果输入是数字(已定义),则返回 true。 试图提供规则要求的最小且可执行的代码。任何帮助是极大的赞赏。谢谢!

【问题讨论】:

  • 如前所述,这是不可能的。 test 仅在 add 完成并且其调用已解析为号码 11 后才能看到结果。您必须在链条的早期检测到它,例如add(test(5, 6)),但我不知道这是否是您要寻找的
  • 这实际上是编程合约问题的简化版本。我的 test() 函数接受 3 个参数:前置条件、后置条件(返回类型)和函数本身。我需要函数的参数来检查是否满足先决条件,即 5 和 6 都是数字。
  • 那么请发布实际代码,而不是简化代码
  • javascript 闭包可以提供帮助
  • 这可以工作function add(x, y) { return x + y; } function test(func, p1, p2) { console.log(arguments) < console.log(func(p1, p2)); } var x = test(add, 5, 6);

标签: javascript function arguments


【解决方案1】:

这是一个只需要您更改test 中的代码的解决方案(除了您调用测试我已将(isNumber, isNumber) 替换为[isNumber, isNumber] 的地方)。

您无需执行任何特殊操作即可访问add 的参数,因为您在test 中创建函数并将其返回以供console.log(add(5, 6)); 调用。

在任何函数中使用arguments 都会将函数的参数作为数组提供。

func(... arguments); 中的 ... 是展开操作,它接受一个数组并将其展开到位。见spread operator

function test(precondition, postcondition, func)   {
   // Extract arguments of func which in this case should be 5 and 6
   // This is required to check whether isNumber(5) and isNumber(6)
   // both return true, so that precondition is met  
  return function() {
    for (const i in arguments) {
      const argi = arguments[i];
      const precondition_i = precondition[i];
      console.log('precondition['+i+'] met: ' + precondition_i(argi));
    }
    const r = func(... arguments);
    console.log('postcondition met: ' + postcondition(r));
    return r;
  };
 }

var add = test([isNumber, isNumber], isNumber, function add(x, y) {return x+y; });

console.log(add(5, 6));

或者不使用arguments... 并且不将数组作为precondition 传递的不太通用的解决方案:

function test(precondition, postcondition, func)   {
  // Extract arguments of func which in this case should be 5 and 6
  // This is required to check whether isNumber(5) and isNumber(6)
  // both return true, so that precondition is met  
  return function(x, y) {
    console.log('precondition met for x: ' + precondition(x));
    console.log('precondition met for y: ' + precondition(y));
    const r = func(x, y);
    console.log('postcondition met: ' + postcondition(r));
    return r;
  };
 }

var add = test(isNumber, isNumber, function add(x, y) {return x+y; });

console.log(add(5, 6));

【讨论】:

  • 谢谢!这很有效,对于延迟的响应感到抱歉。我可能对此有更多疑问:)
  • 太好了,如果您对此还有任何疑问,请告诉我。
  • 好的,所以方法返回了一个函数。如果不满足前置条件或后置条件,我只想打印错误消息(我知道怎么做)并返回。如果我将上述方法称为 add(5, "six"),则结果将打印出我不需要的 5six。只需打印一条错误消息并返回。如果违反了先决条件或后置条件,我可以通过返回“”来创建解决方法。这只是在最后放置一个空行。但我想知道是否有更好的方法来处理这个问题。谢谢!
  • 我认为如果不满足前置/后置条件,只返回"" 是有意义的。想到的另一个选择是执行throw "Conditions not met.";,但是在调用add(5, 'six') 之后的代码将无法到达,因此这可能不是要走的路。
  • 谢谢。所以只是为了确认,您能够调用 add(5, 6) 的原因是因为变量 add 是对函数测试返回的函数的引用?由于函数 test 返回一个匿名函数到变量 add 我们可以使用 add(5, 6) (或其他参数)调用这个返回的函数。这是正确的吗?
【解决方案2】:
var test=function(d,c){
    this.a=d;
    this.b=c;
    this.add=function(){
        return this.a+this.b;
    }
}
var my_test=new test(5,6);
console.log(my_test.a);   // return 5
console.log(my_test.b);  // return 6
console.log(my_test.add());  //return 11

【讨论】:

    【解决方案3】:

    尝试在add 内部调用test 并仅传递add 的参数而不是函数调用:

     function add(a,b){
         test(a,b)
         //and some code
     }
    

    【讨论】:

    • 是的,原始问题与此非常相似。我的问题是,在测试内部,我如何访问 add 的参数?如何访问 5 和 6?
    • @AlexanderNenartovich testadd 函数中被调用?
    • 不,add() 是作为参数传递给 test() 的函数。检查 add() 的结果并不重要。我需要做的就是检查 add() 的两个参数是否都是数字,我需要在 test() 函数中这样做。这就是为什么我需要访问 add() 的参数。除非有一些解决方法。谢谢。
    猜你喜欢
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 2012-10-17
    • 2013-11-06
    • 1970-01-01
    • 2013-11-19
    相关资源
    最近更新 更多