Hello, everyone. It’s Nick from the LASTC, and today we’ll be finishing what we have learned from Chapter Four of John Pollock’s JavaScript: A Beginner’s Guide. Last time, we covered functions and how to create them. Well, today we’re going to learn how to call functions. Calling functions can be relatively simple or frustratingly complex. It all depends on how long the function might be or how many different functions are called within another function. That’s right, functions can exist within other functions. But before we dive into that subject, let’s create a basic function call. As you most likely know, we must declare the function before we can do anything else with it. We’ll also assign it a variable:
var mybrandnewcar = “I want a Volkswagen”;
function send_alert () {
window.alert(“Nick complains about his car too much!”);
}
send_alert();
I have just told the browser to create a pop-up window so all of my viewers can know that I whine too much about needing a new car. The pop-up message is contained within the brackets, but the actual call for the function is nothing more than the function’s name:
send_alert();
As a reminder, always put a semicolon at the end of the function call, or else the browser won’t recognize the code. Now what if you want a place one or more functions within another function? Luckily, creating functions within functions isn’t that difficult. Even so, Pollock warns the reader that JavaScript coders must always “define the function that will be called” before defining “the function that calls it”. Allow me to demonstrate what Pollock means by creating an alert message. First, I write the function with an actual alert message:
function virus_alert () {
window.alert (“Shut it down! Shut it down before they steal your identity and find out where you live!”);
}
After that, I write a second function to call the alert message:
function virus_warning () {
virus_alert ();
}
Finally, I call the function:
virus_warning ();
As you can see, I have basically created a secondary function to call the primary function. However, we can take functions another step further. What if I wanted multiple alert messages to pop up one after the other? In order to do that, I would create multiple functions similar to the virus_alert, and then I would create one secondary function to call all the different functions that came before. Is any of this starting to sink in? Believe me, this may be seem difficult to understand, but just imagine how difficult it is for a newbie such as myself to try and explain these concepts. Don’t get me wrong. I’m not insinuating that the blind are leading the blind, but it does take an unholy amount of time to mentally process these chapters and then spit them back out in these blogs. Still, we’re only a quarter of the way through this book, so I have my work cut out for me. My current goal is to get through this book before the end of October, so I can start blogging about another software skill/program. My heart is set on Dreamweaver. Join me next time when we move on to Chapter Five!
{ 0 comments… add one now }