First of all, let’s understand what the term Immediate Function refers to. It is a sintax that enables you to execute a function as soon as it is defined. This pattern is used to wrap an amount of work without leaving any global variables behind. Here is an example:
Notice the bookstore parameter in line 1. We use this variable thoughout the Immediate Function to append new operations to it. In line 25 we pass a new object to the bookstore and keep it in a global variable window.bookstore, which enables us to access bookstore anytime in our application. In addition, after the function is executed the variable books is discarted maintaining the global space clear.
Now, imagine later on the project we need additional operations such as sortByTitle and filterByAuhtor. This can be easily done by creating a new file and appending new operations to the bookstore object, such as the example below:
That’s it guys.