JavaScript_A Beginner's Guide - Understanding Functions - 09/22/2012

 Functions can make your scripts more portable and easier to debug. This chapter covers the basics of using functions.

A function is basically a little script within a larger script. Its purpose is to perform a single task or a series of tasks. What a function does depends on what code you place inside it.

Why Functions Are Useful
Functions help organize the various parts of a script into the different tasks that must be accomplished. By using one function for writing text and another for making a calculation, you make it easier for yourself and others to see the purpose of each section of the script, and thus debug it more easily.

Another reason functions are useful is their reusability. They can be used more than once within a script to perform their task. Rather than rewriting the entire block of code, you can simply call the function again.

To declare a function, you use the reserved word function, followed by is name, and then a set of parentheses:

function functionname()



The reserved word function tells the browser that you are declaring a function and that more information will follow.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章