0

What does

 private _someVar: (value: any) => void = () => {};

mean?

I think it's "a variable with type as a function that can take any value and return type void" , but what's the deal with = () => {};?

Is it the declaration of variable, like assigning an empty function to it?

If so, why would someone do that?

Can you please provide me some real life example to do so?

Also, it would be really helpful to have few more such examples to make me more comfortable with such declarations.

3
  • () => {} is a function that doesn't do anything. It's the initial value of the variable. Commented Jul 9, 2018 at 18:25
  • A use case: a property that specifies an event handler. By default, nothing happens when the event occurs; but it could if the consumer of the class sets one. Commented Jul 9, 2018 at 18:30
  • You guessed what it does correctly. I can't answer why they need an empty function. Commented Jul 9, 2018 at 19:20

2 Answers 2

1

You are correct. It's a function that takes an any and returns void.

The = () => {} is a default value, assigning an empty function to it, so that if it is not overwritten, it can still be called but it won't do anything.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but what could be the place we need such function ?
You could use it to set a callback, for example. The usage does seem a bit weird. The only reason I can think of to do it this way is so that you don't have to check if the function is set before calling it. For insight into why the author implemented it this way, you would have to ask the author.
0

This is an arrow function. Arrow functions are a short syntax, introduced by ECMAscript 6, that can be used similarly to the way you would use function expressions. In other words, you can often use them in place of expressions like function (parameters1 , parameter 2,....) {syntax......} .but in arrow function you can write (parameter1, parameter2 ,...) => (syntax)

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.