How to Get All Arguments of a Function as Single Object Inside That Function

Is it possible to get all arguments of a function as single object inside that function?

For modern Javascript or Typescript:

class Foo {
reallyCoolMethodISwear(...args) { return args.length; }
}

function reallyCoolFunction(i, ...args) { return args[i]; }

const allHailTheLambda = (...args) => {
return args.constructor == Array;
};

const x = new Foo().reallyCoolMethodISwear(0, 1, 2, 3, 4);
const y = reallyCoolFunction(3, 0, 1, 2, 3, 4, 5, 6);
const z = allHailTheLambda(43110, "world");

console.log(x, y, z); // 5 3 true

For ancient Javascript:

Use arguments. You can access it like an array. Use arguments.length for the number of arguments.

Get a list of all function parameters from inside the function

You can also return the environment at the beginning of the function:

xf <- function (a, b="Hi", c=TRUE) {
as.list(environment(), all=TRUE)
}

Result:

> xf(a=1)
$a
[1] 1

$b
[1] "Hi"

$c
[1] TRUE

How to pass all arguments as collection to another function and not as single argument?

You can use the .apply() method to call a function and pass the arguments as a set.

callSubFunction.apply( this, arguments ); 

The first argument will set the value of this in the allSubFunction method. I just set it to the current this value. The second is the collection of arguments to send.

So your handleCall() function will look like:

function handleCall() {
//set the value of "this" and pass on the arguments object
callSubFunction.apply( this, arguments );
}

It isn't required that you send an Arguments object. You could send an Array of arguments if the circumstance required.

Object destructuring default parameters get all arguments

Arguments vs. Parameters

For this question, it's important to distinguish between arguments and parameters. Arguments are what is passed to the function when it's invoked, without consideration for how the function destructures or sets defaults to those values. So when you call getConfig() with no arguments, and log arguments.length, it should be 0, regardless of how getConfig destructures or sets defaults.

Parameters on the other hand are the variables in the function definition, that it uses. They can be assigned defaults or they can be derived from a destructured argument.

It's a little unclear what you're looking for, whether it's an object that reflects the parameters that the function uses or whether it's an object that reflects the values passed to the function (including destructured undefined variables). I'll go over both cases.

Parameters

To reconstitute the parameters, a rather clunky way is to use the destructured variables into a new object:

import {defaultOptions} from "../defaults.js"

export function getConfig({
width = defaultOptions.width,
height = defaultOptions.height,
fontsize = defaultOptions.fontsize
} = {}) {
// just assign them to parameters
var parameters = { width, height, fontsize };
console.log(parameters);
}

Alternatively, if defaultOptions represents all of and only the parameters passed to the function (i.e. it only has width, height, fontsize), you can assign them with spread syntax:

import {defaultOptions} from "../defaults.js"

export function getConfig(options = {}) {
var calculatedParameters = { ...defaultOptions, ...options };
var { width, height, fontsize } = calculatedParameters;
console.log(calculatedParameters);
}

Values passed to the function (including destructured undefined variables)

To reconstitute the values passed, you can destructure the argument and then assign it again to a variable. This variable is nearly identical to the argument itself. In the below example calculatedArguments is almost identical to options, except now it will have properties for width, height, and fontsize, even if they represent undefined values:

import {defaultOptions} from "../defaults.js"

export function getConfig(options = {}) {
var { width, height, fontsize } = options;
var calculatedArguments = { width, height, fontsize };
console.log(calculatedArguments);

// can also assign width, height, fontsize with default values
({
width = defaultOptions.width,
height = defaultOptions.height,
fontsize = defaultOptions.fontSize
} = options);
}

Return sum of all arguments passed to function

Other people provided answers with redundant copying of arguments to an array that is to be thrown away in a moment.

Instead you can do everything in one step:

function sum() {
return Array.prototype.reduce.call(arguments, function(a, b) {
return a + b;
}, 0);
}

If using ES2015 is an option you can have slightly nicer (subjective) implementation:

const sum = (...args) => [...args].reduce((a, b) => a + b, 0);


Related Topics



Leave a reply



Submit