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

developer writing code at his laptop with code surrounding him in multicolored smoke
Code

Exploring the CSS Properties and Values API

The CSS Properties and Values API is an exciting part of the CSS Houdini suite of APIs that enables developers to define and register custom CSS properties directly in JavaScript. This API introduces advanced capabilities like type checking, default values, and control over whether custom properties inherit their values. These features significantly enhance the power and flexibility of CSS in modern web development.

Developer sitting outdoors at a coffer shop working on his laptop with colors swirling
Code

Exploring the CSS Paint API: Unlocking Creativity in Web Design

The web is constantly evolving, and with it, the tools available to developers and designers expand. One of the most exciting additions to modern web design is the CSS Paint API (also known as Houdini’s Paint API). This feature allows developers to create dynamic, programmatically generated images directly in CSS without the need for external assets or heavy graphical libraries.

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.