Functions help us to write less code and stops us from repeating our code.
You may wonder how can we create an object out of a function. Functions are used to wrap repeating code right?. But in JS , we can use it to create objects using the new keyword. To do this we need something called function constructors.
As we can see in the above image, our myFunction in the window object have a property called name which have the property name as “myFunction”
This function will be remain as a normal function until it is called with the new keyword. When we call the function with new keyword, the function’s prototype.constructor method is called which can instantiate new objects from this function.
Since functions are a type of objects, we are able to attach properties to the functions. As you can see in the above image, there are several properties and methods in the “myFunction” function.
We can use the function’s prototype property to store our methods which are common to all objects created using this function. This will help to save all the methods in one place and helps us to reduce the memory footprint of our objects since our methods are stored in the prototype not in the individual objects.