Writing a JavaScript Function: The Usual, The Okay and The Complicated

I'm Kenneth and I am software engineer. Here to share my experiences to the world and to learn from everyone as well.
Search for a command to run...

I'm Kenneth and I am software engineer. Here to share my experiences to the world and to learn from everyone as well.
No comments yet. Be the first to comment.
In this series, I'd be talking about 3 different approaches to some common coding tasks. The usual followed by a stepped-up one. Then lastly, something out of the ordinary. Excited? So am I!
This post is part of a series that explores how flexible programming languages and frameworks can get. We all know that with any given task, there are several approaches available to achieve it. Let's look at this in action! In this post, we will l...
When defining application components, a good strategy is to split by functionality. In a blogging application, we'll have a component to represent the WYSIWYG editor then, another to hold the publish settings. In this particular example, the publish ...

In this post, I'll show you how to implement feature management on your React applications. Feature management is the way to control which features are available based on certain scenarios. Why Do Feature Management? Here are some scenarios where con...

When I create content, I start off with a topic. Then, I create different content pieces revolving around that topic targeting different platforms like Hashnode, Dev.to, or Twitter. In fact, I recently Tweeted about this, that content creation nowada...

In this post, we'll go through the process of creating an API built using Express and MongoDB. We'll cover the ff. steps: Setting up MongoDB Creating an Express application Optimizing your Express routes Handling errors Let's start! Setting up Mong...

In a React + React Router environment, routing configuration is a one-to-one mapping between a route and that route's display elements. Here's a basic example: <Route exact path='/' component={Home} /> <Route exact path='/drafts' component={DraftList...

This post is part of a series that explores how flexible programming languages and frameworks can get. We all know that with any given task, there are several approaches available to achieve it. Let's look at this in action!
In this post, we will look at the different approaches in implementing one of the basics in JavaScript: declaring functions. There's probably more than 3 ways to declare functions in JavaScript, it's a fairly flexible language, but, I carefully selected three unique ones that better matches the ff. categories: The Usual, The Okay, and The Complicated.
function multiplyNumbers(a, b) {
return a * b;
}
We all know about this one. This is simply following the very basic function declaration syntax. Easy to understand and very straightforward. I don' think I need to explain this further. Let's step it up a bit.
(function(win) {
win.multiplyNumbers = function(a, b) {
return a * b;
};
})(window);
Now we're onto something. This approach introduces a new layer of scope. A wrapper anonymous function accepts the window object. Inside the anonymous function, the actual multiplication function is assigned to the multiplyNumbers variable of the window object. Since the inner function is attached to the window object, anyone outside of the anonymous function will still be able to access it by simply using the normal syntax:
multiplyNumbers(5, 6);
One benefit of this approach is that we can limit the scope of certain variables. Here's another example:
(function(win) {
var PIConst = 3.14;
win.getCircumference = function(radius) {
return 2 * PIConst * radius;
};
})(window);
In this example, a constant variable is introduced to represent the value of PI. Again, the getCircumference function is still accessible to everyone, but no one will have access to the PIConst variable.
Now, let's move to something more exciting.
We've looked at the basic function declaration syntax as well as the anonymous approach. What will happen if we combine both? Let's go crazy!
function getCircumference(radius) {
var PIConst = 3.14;
return (function(radius_internal) {
return 2 * PIConst * radius_internal;
})(radius);
}
The above approach is pretty much the same as The Okay one. But, instead of an anonymous function wrapping the actual function, I inverted it.
The getCircumference function is not performing the work. It's merely returning an anonymous function that is the one who will do the computation. In terms of the execution, everytime the getCircumference function is called, the internal anonymous function will be executed first. Then, the anonymous function's result will be bubbled out. Looks fun right?
There's most likely lots of approaches to declaring functions in JavaScript. Feel free to share them by responding to this post.
I hoped you enjoyed exploring these different approaches with me. I'll be posting more in this series (covering other programming languages too!) so make sure you keep an eye!