1108 Part IV . JavaScript Core Language Reference (Com web hosting)

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 } }

Enter an input value:

Results:

This function is designed to be generalizable, accepting only the input value (n) as a parameter. In the form, the onClickevent 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 func tion 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 .jslibrary 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 objects are not user-interface elements on the page but rather the kinds of objects that may contain data and script func tions (behaving as methods) whose results the user can see displayed in the browser window.
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Leave a Reply