Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Create reusable routines in JavaScript

Tired of rewriting everything? Here's how to build a component library for JavaScript and reuse common routines -- simply by cutting and pasting code

  • Print
  • Feedback
Professional programmers live by an old adage: "Write once, use many times." When you develop a scheme for doing something in JavaScript -- or almost any programming language, for that matter -- you should save your efforts for use the next time a similar problem crops up. You never know when you'll need that random number generator, array sorter, or whatever it is you've worked on for the last two weekends in a row.

JavaScript supports user-defined functions, which is highly advantageous for developing a library of reusable code. You can place code you think you might reuse into one or more functions and save those functions in a text file for future reference. Then, when you need one of the functions, you can copy and paste it into your JavaScript program. To call the function, you must also add a statement elsewhere in your JavaScript program.

When learning a new programming language, one of the first things you should do is create commonly used "plug-and-play" routines. If you're new to JavaScript, you might want to consider starting with a half-dozen or so simple routines that you think might be helpful. Even if you don't end up using them, you'll learn a lot about JavaScript in the process.

This column introduces the concept of creating these so-called plug-and-play routines that can be ported from one JavaScript program to another. It also demonstrates the concept of reusable routines by presenting a number of ready-to-go functions you can use in your own JavaScript programs. Note that the functions are not meant to be cure-alls; some could even be improved. Rather, they are presented as readily editable functions. You are free to enhance or modify any routine presented here and incorporate it into your own library.

Create and use "plug-and-play" routines

To create a reusable plug-and-play routine, simply enclose it within a function in a JavaScript program. After it has been fully debugged, you can copy the function to a library file that you keep on your computer's hard-disk drive. When you want to use the function, simply open the library file, copy it, and paste it into your current JavaScript project.

Recall that JavaScript is an interpreted language, and that unlike Java and other interpreted languages, it does not directly support a #include statement. With the #include statement you can define one or more library files that contain functions you want the main program to refer to. You will need to supply the actual function as part of your JavaScript program, either in the same HTML document that contains the JavaScript code, or in a .js file that contains the JavaScript for the document. (The .js file is used with Netscape Navigator 3.0 and later and is defined with the <SCRIPT SRC="filename.js"> construct. See the JavaScript documentation from Netscape for more information.)

Note that with either method, the browser must download the entire JavaScript program so that it can be executed. Stuffing the JavaScript code into a .js file does not save time or provide added security. And it is a bad idea to place all of your reusable routines in a .js file if you aren't using them in your program; unused routines waste bandwidth. It is far better to selectively copy and paste only those routines you use for each program you write.

  • Print
  • Feedback

Resources