Alessandro Sbenaglia
Alessandro Sbenaglia

Reputation: 153

I need to pass arguments from outer function to inner function

I need to use variable arguments to inner function.

I have this function that I cannot change:

function SUM() { 
  var res = 0;
  for (var i = 0; i < arguments.length; i++) {
    res += (arguments[i]==null?0:arguments[i]);
  }
  return mSum(res);
}

Well because this function sometimes returns extra decimal values, I want to wrap it into a function like

function MySUM( return parseFloat(SUM().toFixed(10))    );

the problem is that SUM function cannot read "arguments" of outer function that calls it, and I don't know how to pass this arguments from mySUM to inner SUM function. What I expect is some like this

function MySUM( return parseFloat(SUM(arguments).toFixed(10))    );

but it doesn't work.

Upvotes: 0

Views: 99

Answers (2)

Mureinik
Mureinik

Reputation: 311228

You can spread the arguments using the ... operator, send them to the SUM function:

function MySUM() {
    return parseFloat(SUM(...arguments).toFixed(10));
    // Here --------------^
}

Upvotes: 1

Devashish
Devashish

Reputation: 1290

You can do it like this:

function MySUM(arguments) {
    return parseFloat(SUM(arguments).toFixed(10))
}

Upvotes: 1

Related Questions