Function Literals

Share This Post

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

More To Explore

Code

Demystifying Scrum User Stories Confirmation: Ensuring Quality and Collaboration

One of the key elements of Scrum is the use of user stories to define the requirements of a system or feature from the perspective of the end user. As teams work through the product backlog, it becomes crucial to confirm the user stories to ensure they meet the desired criteria and are ready for development. In this blog post, we’ll explore the concept of Scrum user stories confirmation and its significance in delivering high-quality products.

Code

The Power of Conversations in Scrum User Stories

At the heart of Scrum lies the concept of user stories, which serve as a means of capturing requirements from the perspective of end-users. However, the true power of user stories lies not just in their written form but also in the conversations that take place around them. In this blog post, we will explore the significance of conversations in Scrum user stories and how they contribute to the success of Agile projects.

Do You Want To Boost Your Business?

drop us a line and keep in touch

Scroll to Top
small_c_popup.png

Need help?

Let's have a chat...


Login

Jump Back In!

Here at Webolution Designs, we love to learn. This includes sharing things we have learned with you. 

Register

Begin Your Learning Journey Today!

Come back inside to continue your learning journey.