FunctionsUnit 6 |
|
Functions in <head> |
|
This sample looks like sample 5-2-functions. The difference is that the functions have been moved to one JavaScript file, which is in the <head> and the function calls are in a separate JavaScript file in the <body> The JavaScript file sample-6-1-functions-in-head.js contains: function sayHello() { alert("hello"); } function saySomething(it) { // use an empty string if it is undefined var aString = it == undefined ? "" : it ; alert("The word is: " + aString); } The JavaScript file sample-6-1-in-body.js contains: sayHello(); saySomething("What's up Doc?"); saySomething(); This is a good organization with the functions in the head and the code that builds the body content in the body. |
|
Three blank lines |
|
This sample is similar to sample 6-1. The JavaScript file sample-6-2-functions-in-head.js contains: function threeBlankLines() { document.write("<br /> <br /> <br />"); } The JavaScript file sample-6-2-in-body.js contains: threeBlankLines(); This is a good organization with the functions in the head and the code that builds the body content in the body. |
|
Return value |
|
This sample shows the use of a return value.
The JavaScript file sample-6-3-functions-in-head.js contains: function twice(value) { return 2 * value; } The JavaScript file sample-6-3-in-body.js contains: var result; var start = 4; result = twice(start); alert(result); // displays 8 |
|
Reading assignment |
|
Reading assignments are in the text book, Java Script, A Beginner's Guide, Second Edition, by John Pollock; McGraw Hill / Osborne, ISBN 0-07-222790-7 Read Module 4.
Much of this chapter is review of programming fundamentals. Alternate reading assignments are in the text book, Java Script Concepts & Techniques Programming Interactive Web Sites, by Tina Spain McDuffie; Franklin, Beedle & Associates, ISBN 1-887902-45-7 Read Chapter 8, from the beginning through Variable Scope (Global vs. Local)on page 233. We will read the rest of the chapter later. |
|
Lecture notes |
|
Do NOT read the lecture notes before hearing the lecture. If you do, you will find the lecture very boring. Read the lecture notes if you do not attend the lecture, or if you wish to review the material. |
|