Javascript create optional callback function

Javascript create optional callback function

Author: antovarlam Date of post: 30.06.2017

I have coded a function, that has 3 arguments, one of them is a callback function. How do I make the callback an optional argument without having to code another function without the callback argument?

If you want more control over invoking it, use call , apply , bind whatever achieves your goal. By posting your answer, you agree to the privacy policy and terms of service. Stack Overflow Questions Developer Jobs Documentation beta Tags Users.

Sign up or log in to customize your list.

javascript create optional callback function

Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us. Log In Sign Up.

Optional callback in javascript (Example)

Join the Stack Overflow Community. Stack Overflow is a community of 7. Join them; it only takes a minute: Simple, just check if callback is defined before you call it.

How to use optional arguments in functions (with optional callback)

Lucas Green 3, 13 Ensure the callback is a function, and then invoke it. Much prefer the style of Mythril's answer.

No, I just don't like the way you've written it on a single line. As a user of someone else's interface, I would want to know if the value I inserted was not the expected value, rather that just having it ignored.

javascript create optional callback function

I would still expect say 'hello', 'world' to error out. Otherwise I'll never know that isn't what the function expects. Hopefully people aren't fishing around with different arguments to infer a function's argument signature though: Sign up or log in StackExchange.

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.

Do's and Don'ts ยท TypeScript

Stack Overflow works best with JavaScript enabled. Will error if they pass an object that doesn't implement [[Call]]. MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3.

javascript create optional callback function

Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.

inserted by FC2 system