Team LiB
Previous Section Next Section

Summary

JavaScript functions are the developer’s most basic tool for creating structured reusable code. A function should be designed to be self-contained and pass data in through parameters and back via the return statement. In general, most parameters are passed to a function by value, but composite types such as arrays and objects are passed by reference. JavaScript functions are very flexible and a variable number of parameters can be passed to a function. However, some programming caution should be employed, given JavaScript’s lax type and parameter checking. Further, to ensure reusable functions, local variables should be declared with the var statement to avoid conflicts with global names. Local or hidden functions can also be used to hide calculations internal to a particular function. Complex tasks can be broken up into multiple functions since JavaScript functions can of course call one another. Recursive functions can be used to create elegant solutions that perform calculations by having a function call itself over and over again. While JavaScript functions are very powerful, they are ultimately implemented as objects—an even more useful construct discussed in the next chapter.


Team LiB
Previous Section Next Section