Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting Arguments from a Function Passed as a Parameter to Another Function in JavaScript

I am new to JavaScript. I have a small program where a function takes another function as a parameter. I am trying to extract/access the arguments of the function passed as a parameter. Here's an example:

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 is a function that returns true if an input is a number (already defined). Tried to provide a minimal and executable code as required by rules. Any help is greatly appreciated. Thank you!

like image 944
Alexander Nenartovich Avatar asked Mar 16 '26 04:03

Alexander Nenartovich


1 Answers

Here is a solution that only requires you to change the code in test (with the exception of your call to test where I've replaced (isNumber, isNumber) with [isNumber, isNumber]).

You don't have to do anything special to get access to add's arguments because you create the function inside test and return it to be called by console.log(add(5, 6));.

Using arguments inside any function will give you the function's arguments as an array.

The ... in func(... arguments); is the spread operate which takes an array and expands it in place. See 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));

Or a less generic solution that doesn't use arguments and ... and doesn't pass in an array as 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));
like image 136
Rocky Sims Avatar answered Mar 17 '26 19:03

Rocky Sims



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!