function that takes function as an argument javascript code example

Example 1: pass function as argument typescript

class Foo {
    save(callback: (n: number) => any) : void {
        callback(42);
    }
}
var foo = new Foo();

var strCallback = (result: string) : void => {
    alert(result);
}
var numCallback = (result: number) : void => {
    alert(result.toString());
}

foo.save(strCallback); // not OK
foo.save(numCallback); // OK

Example 2: js running function as parameter

function FuncOne(param1) //example function
{
  //Do whatever
}

function FuncTwo(FuncOne, param1) //Function to call FuncOne w/ param
{
  FuncOne(param1); //Write it like you would any normal function call
}

Tags:

Misc Example