I showed in the previous post how we can add event handling functionality to our JavaScript code. The example I used showed how we can attach a function to an event such as window.onload. This works well, but as you get more advanced in your JavaScript, you will want to do more. For example, you will want to “wire up” your program with some initialization events and have the initialization occur when you load your program. We want to do this in an object oriented manner to make our code easy to maintain. So we could do something like this…
[code lang=”js”]
function init(){
// Do some initialization stuff
}
// Initialize our app
window.onload = init();
[/code]
While the aforementioned code block works, you might find it becomes unwieldy quickly. Our init function would grow large rather quickly. This would begin to violate Single Responsibility Principle (SRP). Now, you might be saying to yourself, “Hold on…it does not violate SRP as my init function does one thing and one thing only…inits my app!” Ok, if that is the way you want to look at it, that is ok, but that does not solve the issue of writing this huge init function. Have fun with maintenance. SO what do I suggest? Modularization of code with a function literal call from onload. Here is how it would work…
[code lang=”js”]
// Create our init functions
function initCore(){
// Do some stuff to initialize the core functionality
}
function initUser(){
// Do some stuff to initialize the user items
}
function initSomethingElse(){
// Do some other stuff that does not belong in the other inits
}
// function literal to the onload event
window.onload = function(evt) {
// Call init functions
initCore();
initUser();
initSomethingElse();
}
[/code]
As you can see, this method allows for separation of functionality into their own functions and the ability to call all of them with one event. Notice I am using a function literal in my onload event. I do not have a function name or anything. I am just telling my app that when the window loads, I want to run a function that contains other functions. Now you could do this another way…
[code lang=”js”]
// Create our init functions
function init(){
// Call init functions
initCore();
initUser();
initSomethingElse();
}
function initCore(){
// Do some stuff to initialize the core functionality
}
function initUser(){
// Do some stuff to initialize the user items
}
function initSomethingElse(){
// Do some other stuff that does not belong in the other inits
}
// function literal to the onload event
window.onload = function(evt) {
// Call main init function init();
}
[/code]
At this point, it is more of a stylistic opinion, but they both accomplish the same thing…separate functionality into modules and tie them together with one event using a function literal.
There is one other advantage to utilizing function literals and that is the event object. Notice the “evt” argument parameter passed in the function literal. What is that? That is the DOM event object. By passing this into our function literal, we can have access to the DOM event object within our function literal if we need it.
This is some more advanced stuff if you are just beginning your JavaScript journey. So if you are reading this and do not quite “get it”, don’t worry. Keep plugging away and you will reach a point where you will use events and function literals before you know it.
Happy Coding!
Clay Hess