Reputation: 46
It's one of those times where I want to do soemthing, but I'm not sure what it's called...
Hopefully, someone can help!
I have the following function:
function myfunction(object1, object2) { ... }
I want to pass another function onto object1 specifically using the .click method.
I can get this working easily with only one object within the function using the following:
function myFunction(object1) { ... }
$('button').click(function() {
// Passes along another function!
myFunction(anotherFunction());
});
How would someone approach this when there are 2 objects? I can't seem to get anything working. Any ideas? Or am I approaching this the wrong way?
Upvotes: 0
Views: 43
Reputation: 5522
var a = 5,
b = 2;
function check(val1, val2) {
console.log(val1);
console.log(val2);
}
function add() {
return a + b;
}
function mulitply() {
return a * b;
}
check(add, mulitply); // this will send refernce of function's not output
check(add(), mulitply()); // this will converts into like this check(7,10);
Upvotes: 0
Reputation: 197
Updated Answer
Assuming still:
function myFunction( function, anotherFunction, ... ) { ... }
If you want to pass specific arguments but be able to omit arguments, you could provide an argument but catch it as falsy:
myFunction( null, someOtherFunction, maybeAnotherFunction )
You then would need to handle the null
, perhaps:
function myFunction( function, anotherFunction, ... ) {
let fnc = function;
let fnc2 = anotherFunction;
let ... = ...;
if(fnc) ...
if(fnc2) ...
if(...) ...
...
}
Original Answer
Because you are triggering the function immediately during its passing you might actually want to just send it without initializing it. Try the below and see if this works for you.
function myFunction(object1, object2) {
object1()
object2()
}
$('button').click(function() {
// Passes along another function!
myFunction(anotherFunction1, anotherFunction2);
});
Upvotes: 1