1

Is there a way to set a default for a function passed as an argument to another function. By default i mean a function that does absolutely nothing.

So i can check if the function argument value is null and assign it to an empty void function:

MyFunction(someVar:string,callback:()=>void){
 if(callback==null) {this.MyEmptyFunction();}else{callback();}

}

MyEmptyFunction():void{}

Is a way to avoid creating an empty function and the null check?

1 Answer 1

3

You can set a default just as you would for any other parameter:

function MyFunction(someVar:string, callback = () => { }){
    callback();
}

Or with the argument type explicitly specified:

function MyFunction(someVar: string, callback: () => void = () => { }) {
    callback();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.