This question already has answers here:
Passing arguments forward to another javascript function
(5 answers)
Closed 9 years ago.
I have a method defined, say abc, which accepts a function name, say xyz and the arguments to that function as its arguments. Now, the abc method needs to call xyz with the arguments received.
xyz can be any method and could be having any number of arguments, also, xyz cannot be using arguments keyword. rather, the arguments for the method should be passed as separate variables.
Ex: xyz will be defined as:
var xyz = function(a,b){//code for processing a and b}
I am calling the abc method with the first argument as the function to be invoked and rest of the arguments as arguments to be passed to the function while calling it. In abc, I am making use of arguments keyword to access the dynamic number of arguments sent. But from there I am not sure how to call the next function with the arguments.
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));
}
Related
This question already has answers here:
Can you rebind a rebound function using `bind`
(3 answers)
How to override 'this' parameter of a bound function in javascript
(3 answers)
Javascript function bind override (how to bind it to another object)
(2 answers)
Closed 4 months ago.
I have a question about Function.prototype.bind.
function f() {
return this;
}
const g=f.bind({foo:'foo'});
const h=g.bind({bar:'bar'});
console.log(h()); // expected {bar:'bar'} but get {foo:'foo'}
I was expecting the above snippet to produce either {foo,bar} or {bar} but instead I get {foo}.
Can you explain what is going on?
Using .bind() to create a function that "fixes" the value of this in the called function results in a function where this cannot be overridden.
What you get back from .bind() is a new function that will pass along arguments to the original target function, with a guarantee that in that called function the value of this will be what you ask for. Re-binding that function therefore does no good: you do get back a new function, but it calls a function (the first bound function) that explicitly ignores its own this value.
This question already has answers here:
JavaScript variable number of arguments to function
(12 answers)
Closed 5 years ago.
I would like to call a function, lets say test_func that accepts variable number of arguments. (like this function http://locutus.io/php/array/array_intersect_assoc/)
Please note I would like to avoid modifying the receiver function! All the answers so far require modifying the receiver function, just as the linked "possible dupes". I would like to programmatically generate the argument list itself. (eg pass variable number of objects)
I do not know in advance how many arguments I will have. How can I generate a variable length argument with objects?
var v1 = {test:1};
var v2 = {test2:2};
var obj_arr = [v1,v2];
console.log(test_func (obj_arr.join(",")));
//in my case this should be the equivalent of test_func (v1,v2);
function test_func (object_arg) {
return(arguments.length);
}
//should return 2!
If you're in an ES5 environment you can use arguments:
function test() {
var param1 = arguments[0];
var param2 = arguments[1];
// arguments is array like, you can iterate over it with a loop
}
If you are in an ES6 environment you can either use the rest operator as suggested by Suren or also use the arguments variant as above - depending on the convention of you're team.
This question already has answers here:
Why provide an array argument in Javascript's array.forEach callback?
(2 answers)
Closed 1 year ago.
I know that forEach in JavaScript calls my callback function with three parameters:
arr.forEach(function callback(currentValue, index, array) {
//your iterator
})
In the above example arr and array are same array and arr exists in the callback function closure.
Now the question is what is the point of passing array to the callback function?
If your callback function were declared elsewhere:
function forEachCallback(value, i, array) {
// ...
}
Then it has no idea what array it's being used for:
someArray.forEach(forEachCallback);
Because the array is passed as the last argument, such a callback has access to it.
The callback need not be in the same scope as the forEach call. In a case like this, the third parameter would ensure that the callback has some reference to that array.
This question already has answers here:
Parameters inside callback function in Javascript
(1 answer)
Higher-Order Functions in JS
(1 answer)
Closed 2 years ago.
I have this code that filters an array using the .filter method.
I'm extremely confused about the function's parameters that is being specified in the .filter method.
Where does the parameter come from? How do I know when to add a parameter like 'value', and what is the value of the parameter 'value'?
var newArray = [1,2,3,4,5,6,7,8,9,10];
newArray = newArray.filter(function(value) {
return value < 6;
});
I'm not too sure if it is the right term to use.
Where does the parameter come from?
It gets passed to the function when the function is called … which will happen somewhere inside filter or a function called by filter.
How do I know when to add a parameter like 'value',
Generally by reading the documentation
what is the value of the parameter 'value'
From the documentation, the first argument is:
element:
The current element being processed in the array.
Array is a JavaScript class and each JavaScript class can have prototype. Filter method is a part of Array prototype and this function is getting as an argument callback function with one argument.
Inside Filter function, your callback is being called on each array item.
You can find more informations about this function in JavaScript docs.
This question already has answers here:
What is the difference between call and apply?
(23 answers)
Closed 7 years ago.
A pattern in some javascript libraries is to be able to pass any number of parameters to a function:
functiona(param1)
functiona(param1, param2, param3)
functiona(param1, param2)
I have an array of unknown length, and I'd like to pass all the array items as parameters to a function like functiona(). Is this possible? If so, what is the syntax for doing this?
What you want is probably Function.prototype.apply().
Usage:
var params = [param1, param2, param3];
functiona.apply(this, params);
As others noted, functiona declaration may use arguments, e.g.:
function functiona()
{
var param1 = this.arguments[0];
var param2 = this.arguments[1];
}
But it can use any number of normal parameters as well:
function foo(x, y)
{
console.log(x);
}
foo.apply(this, [10, 0, null]); // outputs 10
Use arguments:
The arguments object is an Array-like object corresponding to the
arguments passed to a function.
Yep, all parameters passed to a JavaScript function can be accessed using the arguments array within the function.
function foo () {
console.log(arguments[0]); // -> bar
console.log(arguments[1]); // -> baz
}
foo('bar', 'baz');