To create a function declaration start out by typing the function keyword followed by a name for the function, then opening and closing parentheses that might contain one or more optional parameters for the function followed by opening and closing brackets. This way of defining a function differs slightly from a function expression, and arrow functions, as well as other ways of defining a function such as using the Function constructor. There is of course the slight differences in syntax when it comes to just looking at code, but there are also differences in terms of how they behave at run time. More on that later, but for now lets just look at a basic example of a function declaration when it comes to syntax.
So a function declaration might look something like this:
Sure there are some wried ways of defining them that involve passing a string to the Function constructor or making use of eval, but for the sake of this post I do not want to get to far off topic from function declarations.
When it comes to writing what is called a constructor function, a function declaration is a good choice for this kind of function. I will not be getting into detail about this kind of function, as well as many related topics such as the prototype chain, and Object Orientated Programing. However I think A quick hello world style example of a constructor is called for here.
Another kind of function that is at odds with a constructor would be a pure function. There are a few rules that need to be observed when wring these kinds of functions one of which is making sure that the same result is always returned for the same set of arguments. When it comes to constructor functions, and mainly the prototype methods of the class instances that are created with them, the end result that is returned can differ for the same set of arguments depending on the value of the this keyword, and if the new keyword is used or not.
Although a pure function can be written with a wide range of options for creating functions, the job can be done with declarations just fine.
Aside from function declarations the other most common way to go about defining functions before the introduction of arrow functions was the so called function expression or function literal as it is also often referred to as. The function expression has a slightly different syntax that involves typing the function keyword followed by opening and closing parentheses and then the brackets. A function expression is the result of this expression that is typically stored in a variable, and then it is that variable that is called to execute the function.
The same basic function declaration above could then be written like this as an expression then
So function declarations might come off as being a little less versatile compared to expressions, but this feature about them is one reason why I might choose to use them from time to time. When using expressions I need to take care that I am defining expressions towards the top of the module and make sure that any call s for that function are below them.
Another thing about function expressions as that they are generally regarded as a better choice when it comes to working with high order functions. In other words working with a function that will take a function as an argument. The main reason why this might be may have to do with being able to pass one as an argument for a function call without having to declare it to a variable name first.
So expressions can be used like this:
When it comes to declarations I have to well, declare them first.
The main thing about arrow functions is that they handle the situation with the this keyword in a whole other way compared to the other options. I am used to the this keyword referring to a class object instance such as with constructors, and class methods. That is not at all the case with arrow functions.
One will run into problems with arrow function when it comes to using the function prototype methods such as apply, bind and call. Again this has to do with how the this keyword works wit arrow functions, so typically I will only want to use these methods that are ether function declarations, or functions that behave like them at least.
Arrow functions will not work so great when it comes to making a constructor function. The reason why is again because of how these functions work with the this keyword. So then this is yet again another reason why one should stick to using the old tired yet true types of functions, unless they are sure that arrow functions will work without issue.
Another thing that is lost as a result of using arrow functions is the arguments object which is a nice feature to have to work with inside the body of a function. This object can be used to find out how many arguments where given when the function was called, and also it can be used as an alternative way to get the values of such arguments. Say that I want to have a function that can take 0 or more arguments and do something with all the values such as some kind of sum function. This can be done with function declarations as well as expressions by way of the arguments object, however such code will not work in an arrow function.