JavaScript Bible, Gold Edition part 127. This book will bring programmers and non-technical professionals, including casual programmers and scripters, painlessly up to speed on all aspects of mastering JavaScript. Key topics include programming fundamentals, JavaScript language elements and how to use them effectively, and how to easily and efficiently add powerful new functionality to HTML documents and Java applets. | 1108 Part IV JavaScript Core Language Reference Listing 41-4 continued function factorial n if n 0 return n factorial n - 1 else return 1 SCRIPT HEAD BODY FORM Enter an input value INPUT TYPE text NAME input VALUE 0 P INPUT TYPE button VALUE Calc Factorial onClick factorial P Results INPUT TYPE text NAME output FORM BODY HTML This function is designed to be generalizable accepting only the input value n as a parameter. In the form the onClick event handler of the button sends only the input value from one of the form s fields to the factorial function. The returned value is assigned to the output field of the form. The factorial function is totally ignorant about forms fields or buttons in this document. If I need this function in another script I can copy and paste it into that script knowing that it has been pretested. Any generalizable function is part of my personal library of scripts from which I can borrow and saves me time in future scripting tasks. You cannot always generalize a function. Somewhere along the line in your scripts you must have references to JavaScript or custom objects. But if you find that you re frequently writing functions that perform the same kind of actions see how you can generalize the code and put the results in your library of ready-made functions. And if your audience uses browsers from Navigator 3 onward and later versions of Internet Explorer 3 onward consider placing these library functions in an external .js library file. See Chapter 13 for details on this convenient way to share utility functions among many documents. Custom Objects In all the previous chapters of this book you ve seen how conveniently the browser document object models organize all the information about the browser window and its document. What may not be obvious from the scripting you ve done so far is that JavaScript enables you to create your own objects in memory objects with properties and methods. These .