DrNoob
DrNoob

Reputation: 109

How to declare a function that will accept a function with parameters and without parameters in its parameters?

how can i execute 2 different functions passed as a parameter to another function , different means function A have no parameters but function B have one parameter , so how i must to declare function C that will accept function A and B in its parameter ?

function A:

functionA(){
//func A will call itself after some code in function C
this.functionC(this.functionA);}

function B:

functionB(e){
//func B will call itself after some code in function C
this.functionC(()=>this.functionB(e));
}

function C:

functionC(func){
//some code
func;
}

With this declarations functionA() is the only one worked , for functionB()it doesnt work maybe the func parameter in functionC(func) must have a parameter too or the call of func inside this functionC must be with a parameter , what do you think to make both A and B functions execute correctly when called by C ?

Upvotes: 1

Views: 367

Answers (2)

Cat
Cat

Reputation: 4226

This example shows how to pass funcB and its argument into funcC separately and use them inside of funcC.

In your particular situation -- where funcB is not expected to receive more than one argument -- the code would work even if you removed both of the ...s. But as long as you're learning how to use higher-order functions, you might as well learn a more robust way.

The ... in the definition of funcC is called the rest syntax because it gathers up the "rest" of the arguments into an array (named args in this case), which funcC can use.

The ... in the invocation of someFunc (inside funcC) looks the same, but it has the opposite effect. It's called the spread syntax because it spreads the array into a number of separate values. (You can see that this is a good thing in the example because funcB is expecting a number, not an array of numbers.)

The benefit is that we can pass funcC ANY number of arguments -- as long as the first argument is a function -- and funcC will automagically receive an array including any/all arguments that follow. funcC will eventually call someFunc, and when it does, it will spread the array out and pass every single argument along as arguments to someFunc.

(It's up to us to supply the correct number of "additional" arguments for whatever function we pass as the first argument. For funcA, pass no additional arguments. For funcB, pass 1 additional argument, etc.)

function funcA(){
  output += "\nfuncA";
}

function funcB(num){
  output += "\nfuncB with: " + num;
}

function funcC(someFunc, ...args){
  output += "\nfuncC";
  someFunc(...args);
  console.log(output);
}

function funkY(num, str){
  output += "\nfunkY with: " + num + " and " + str;
}

let output = "";
funcC(funcA);

output = "";
funcC(funcB, 42);

output = "";
funcC(funkY, 42, "woo!");

Upvotes: 2

critical_error
critical_error

Reputation: 6706

You could pass both A and B functions as callbacks to C like so...

<!DOCTYPE html>
<head>
    <title>Test</title>
    <script>

        let functionC = function ( callbackA, callbackB ) { 

            // Do some work...

            // If callbackA is a function, call it.
            if ( typeof callbackA === 'function' )
                callbackA();

            // Do some work to get a value to pass to callbackB.
            let value = 'SomeValue';

            // If callbackB is a function, call it.
            if ( typeof callbackB === 'function' )
                callbackB( value );

        };

    </script>
</head>
<body>

    <script>
        // Call functionC with callbacks A and B as parameters.
        functionC( 
            function () { console.log('callbackA called!'); },
            function ( param ) { console.log( `callbackB called with param value: ${param}.`) }
        );
    </script>
    
</body>
</html>

Upvotes: 1

Related Questions