Function Literals

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

computer, laptop, work place-2982270.jpg
Code

Unlocking Wireless Communication: A Dive into the Bluetooth API

Wireless communication has become an integral part of our daily lives, and Bluetooth technology is at the forefront of this revolution, enabling devices to exchange data over short distances and creating a world more interconnected than ever before. At the heart of this technology lies the Bluetooth Application Programming Interface (API), a powerful tool for developers looking to harness the capabilities of Bluetooth in their applications. In this blog post, we’ll explore what the Bluetooth API is, how it works, and the possibilities it opens up for innovation in wireless communication.

lighthouse, beacon, atlantic-8578318.jpg
Code

Understanding the Beacon API: Simplifying Asynchronous Data Transfers

In today’s data-driven world, web applications often need to send data back to the server. Traditionally, this has been done using AJAX requests or similar methods. However, these techniques can come with a cost, especially when dealing with data that needs to be sent during the unload phase of a document, such as tracking and diagnostic data. This is where the Beacon API shines by allowing developers to send data to a server more reliably and efficiently.

Share This Post

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.