Passing Arguments Forward to Another JavaScript Function

Passing arguments forward to another javascript function

Use .apply() to have the same access to arguments in function b, like this:

function a(){
b.apply(null, arguments);
}
function b(){
console.log(arguments); //arguments[0] = 1, etc
}
a(1,2,3);

forward all arguments to another function

...am wondering if there are any downsides to it...

Not really, other than a couple of points to be aware of:

  • arguments in loose mode has some minor performance problems (nothing to worry about in normal situations).
  • Arrow functions don't have their own arguments, so that won't work in an arrow function; you'll either get a ReferencError (if the arrow function is at the top level) or the arguments from the nearest containing function function (or method) (because arrows close over arguments [and this and super]), which may not be what you want.

You're using spread notation, so you might consider using rest notation as well:

function foo(...args) {  return bar(...args);}
function bar(a, b) { return a + b;}
console.log(foo(1, 2)); // returns 3

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.

Passing events and arguments into another function

Pass it an anonymous function:

boxes[i].addEventListener('click', function ( e ) { change(e, 10, 20) }, false);

Pass Arguments from One Function to Another Without Calling It

From your initializeTable function return a function that wraps the applyTableFilters function with the arguments you want.

Then assign the returned function to a var to be executed later.

function initializeTable(options) {
var dynamicTable = new List("table-content", options);
// other stuff

return function () {
applyTableFilters(dynamicTable)
}
}

// other stuff

var applyTableFiltersPrep = initializeTable(options)

// later, when you want to execute...
applyTableFiltersPrep()

JSFiddle example

passing single argument into a function that requires multiple arguments in javascript

JavaScript parameters are optional you don't need to pass them. So you can do something like this:

function multiply(a, b) {
if(typeof b === 'undefined') {
b = 10;
}
return a * b;
}

console.log(multiply(5));
// expected output: 50

In newer versions of JS you can also do default parameters like this:

function multiply(a, b = 10) {
return a * b;
}

console.log(multiply(5));
// expected output: 50

Passing arguments of a function to another function in javascript

Use the apply method of functions to call it with a variable argument list:

function abc(funcName) {
funcName.apply(null, [].slice.call(arguments, 1));
}

You could also pass along your current context by supplying the first argument:

function abc(funcName) {
funcName.apply(this, [].slice.call(arguments, 1));
}

How to pass arguments array to another function in JavaScript?

Take a look at apply():

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

function other(){
test.apply(null, arguments);
}

Passing all parameters of one function to another? (javascript)

Use Function.prototype.apply(thisArg[, argsArray]), like so:

b.apply(this, arguments);

Now, arguments is an array-like object that has some other properties (like callee) apart from its n-index properties. So, you probably ought to use Array.prototype.slice() to turn the object into a simple array of arguments (though either will work in modern environments).

b.apply(this, Array.prototype.slice.call(arguments));

See also: Function.prototype.call()



Related Topics



Leave a reply



Submit