When creating functions, What is the main difference and when to use these ways of creating a function?
onSubmit = () => {}
functionsName : () => {}
When creating functions, What is the main difference and when to use these ways of creating a function?
onSubmit = () => {}
functionsName : () => {}
There's no difference in the creation of the functions, just what is done with the function once it's created:
onSubmit
one creates a function and assigns it to an in-scope variable.functionsName
one creates a function and assigns it to an object property. This form is only valid within an object initializer. (Outside of an object initializer, it's not a syntax error, but it's just a labelled statement and the function is never assigned to anything.)You may find another answer of mine useful as well. It's a rundown of various ways of creating functions.