JavaScript Editor Free JavaScript Editor     JavaScript Debugger 

Main Page

Previous Page
Next Page

4.5. Functions

Fromsome points of view, JavaScript functions are a little bit on the strange side when compared to other programming languages. This is because even though they are functions, they don't necessarily return a value. JavaScript functions are really groupings of code designed to perform a specific task. Quick, imagine yourself writing a JavaScript function that concatenates two strings. Visualize it fully in your mind before looking at the example in Listing 4-8.

Listing 4-8. A Function That Concatenates Two Strings

function concatenate(a,b) {
  return a.toString() + b.toString();

Don't be surprised if the function that you visualized looks remarkably similar to the one in Listing 4-8. There is a perfectly logical reason for this similarity; my mind-reading machine has been perfected. Either that or I'm aware that the majority of developers know only a couple ways to define a JavaScript function. Which is the truth? I'll give you a hint: It is currently the fall of 2005, and I'm writing this on the SEPTA R5 line on my way to Doylestown, Pennsylvania. If I actually could read minds across space and time, I would have won Powerball last week and I'd be writing this on the beach in Tahiti.

This means that, as web developers, we're all in a rut, doing the same thing the same way day after day and year after year. Yeah, I know the drill: "It works, so why change it?" and "I always do it that way" are usually the statements used. To these statements, I have one response, "You learn more from your mistakes than you do from your successes!"

When you actually get down to it, there are several separate and distinct ways to define a function in JavaScript. Why so many ways to define a function? I can't rightfully say, but I can take a guess. It has always seemed to me that the more ways there are to perform a single task, the more flexible the language is and the more problems can be solved.

Getting back to our function that concatenates two strings, we've already seen one possible method of implementing the solution, so let's take a look at another way. JavaScript has the Function() constructor for, interestingly enough, constructing functions. The Function constructor, an example of which is shown here, is used to create a function and assign it to a variable or an event handler.

var concatenate = new Function('a','b','return a.toString()
+ b.toString()');

In addition to the Function constructor, the function operator can be used to assign a function to a variable. One of the more interesting "features" of the Function constructor is that it shows that JavaScript is really an interpreted language because the function is stored as a string. This is an example of our string concatenation example defined using the function operator:

var concatenate = function(a,b) {return a.toString() + b.toString()}

Previous Page
Next Page

JavaScript Editor Free JavaScript Editor     JavaScript Debugger