0

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?

2
  • If you want to pass the function, and not the result of the function, take the () off of the anotherFunction when you pass it.
    – Taplar
    Commented May 2, 2018 at 19:24
  • "pass another function onto object1" does not mean anything to me. If you want to use the result of a function call as the value of this parameters, then your example already does it, and it's trivial to pass aThirdFunction() as the value of the parameter object2, the same way you did with anotherFunction() for object1: myFunction(anotherFunction(), aThirdFunction()). If you want something different, could you explain it differently than "pass a function onto an object"? Commented May 3, 2018 at 14:17

2 Answers 2

1

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);
});
6
  • I'm looking to pass a function onto one of the objects only. For instance, anotherFunction1 to object1... Nothing more. Not sure if that's possible.
    – Matt U
    Commented May 2, 2018 at 19:38
  • I might be confused on the ask. Do you mean you want to be able to omit object1 and still be able to use object2?
    – Staghouse
    Commented May 2, 2018 at 19:41
  • Yes. My primary function (myFunction) actually prints XML data. object1 and object2 are individual filters that customize the data output. I need to be able to pass functions onto those objects individually.
    – Matt U
    Commented May 2, 2018 at 19:46
  • Your explanation is a bit strange to me. When you say passing functions on to object I am not completely sure what that means. In terms of omission of arguments you could need to provide a value that you expect and then reject the expectation. If you provide 1 argument when 2 is expected, the first argument will be run.
    – Staghouse
    Commented May 2, 2018 at 19:54
  • Oh ok. So my question would then be, how does one add a function to a specific argument, when there are two arguments? Would that make more sense?
    – Matt U
    Commented May 2, 2018 at 20:08
0

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);

Not the answer you're looking for? Browse other questions tagged or ask your own question.