Search code examples
javascripthtmlcssfunctionhide

Insert function inside another one by using parameter


How to run this external function inside some outer function by inputing it?

function function1(a){
  a;  
}

somefunction(function (){
  $('#div1').hide();alert('hi');
});


Solution

  • If you want to execute the external function inside an outer function by inputing it, you can pass the reference of the external function to the outer function and then invoke the function in the outer function

    Like this :

    function externalFunction(outerFunctionReference){
      // invoking the given function
      outerFunctionReference();
    }
    
    function firstOuterFunction() {
      console.log("Hello from the first outer function");
    }
    
    // passing the first outer function reference
    externalFunction(firstOuterFunction);
    
    // passing the second outer function reference
    externalFunction(function secondOuterFunction() {
      console.log("Hello from the second outer function");
    });