function keyword is essential to becoming an effective JS coder.
So, let’s look into them one by one and see what the
function might really mean.
Any and all code is enclosed within an anonymous
function. It’s not even stored in a
variable; it’s just called immediately so its content is just executed, now.
window in case of web browsers) which is a fragile namespace, easily polluted by defining things directly at the script level.
So in the example above, the
function is used to confine script’s code and all the symbols it defines. But sometimes we obviously want to let some things through, while restricting access to some others – a concept known as encapsulation and exposing an interface.
What we get here is normal JS object but it should be thought of more like a module. It offers some public interface in the form of
getValue functions. But underneath, it also has some internal data stored within a closure: the
value variable. If you know few things about C or C++, you can easily see parallels with header files (.h, .hpp, …) which store declarations that are only implemented in the code files (.c, .cpp).
Or, alternatively, you may draw analogies to C# or Java with their public and private (or protected) members of a class. Incidentally, this leads us to another point…
Let’s assume that the
counter object from the example above is practical enough to be useful in more than one place (a tall order, I know). The DRY principle of course prohibits blatant duplication of code such as this, so we’d like to make the piece more reusable.
Here’s how we typically tackle this problem – still using only vanilla
Pretty straightforward, right? Instead of calling the function on a spot, we keep it around and use to create multiple objects. Hence the function becomes a constructor for them, while the whole mechanism is nothing else but a foundation for object-oriented programming.
Many of them – likely most – are callbacks to be invoked when some asynchronous operation has finished. Whether it’s an AJAX request in the browser or an I/O call done by node.js on the server, it’s through callback function that we are notified of its completion. The result, if any, is therefore passed as parameter:
Written this way, the asynchronous code reads almost exactly like the synchronous one. However, in general this ceases to be true for any more complicated cases, such as issuing more than one simultaneous call and requiring for any/all of them to complete. And even for non-parallel, sequential algorithms we may quickly encounter the problem of “marching callbacks”, resulting in what is often called a callback spaghetti:
Trying to alleviate that problem by using two-space indents (rather than more sensible four-space ones) doesn’t seem like a satisfactory solution, though. Fortunately, there are some better ways to structure asynchronous computation.
It does so by forming a kind of asynchronous pipeline whose elements are, of course, functions of particular form. Besides getting the current result as argument, those functions also receive a continuation (here,
next) which is called when we’re ready to advance to the next element in the pipeline. Naturally, this continuation is also a function – a special one prepared by
So indeed, there is usually plethora of
function is about.
Adding comments is disabled.Comments are disabled.