Reputation: 3322
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?
Upvotes: 1
Views: 235
Reputation: 249696
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();
}
Upvotes: 3