With the instances of constructor functions there are prototype methods like that of the array splice method for example that will mutate an array in place, and then there are other methods in the array prototype such as slice that will not mutate the source array, but instead return a new array without mutating the source array off of while array slice was called. However even with array slice it can still end up giving different results depending on the state of the array that is is called off of, so although it is pure like it is not the case.
So a pure function is a function where the return value is always the same for the same given arguments, and also there are no side effects when calling the pure function. So When calling the function no global variables or state object is mutated. Only a new return value is returned and then it is a question as to what is done with that return value.
So say I have two distance formula methods one of which makes use of some global variables, and one that just works with what is passed via arguments.
In this section I will be going over a simple Box constructor function that has a single distance prototype method, and a pure function style alternative that does the same thing without the use of a constructor.
So lets say I have a Box Class that looks like this.
So in place of using a constructor function to create a Box Class instance why not just have a pure function that will always return the same box object for the same arguments, and in addition it returns just a plain old object? I can then use the plain old object that has box properties with a stand alone distance formula that is also very much pure function like.
As of late I find myself ditching the use of constructors in favor of writing modules like this.
So pure functions are generally a better way of going about doing things I would say over all. I do not think that I would care to always try to make everything in a project fully functional by trying to make everything a pure function though. To at least some extent it might not always be possible to make all functions a pure function in a project anyway, but still I think they help to keep things a little more sane.