土曜日, 1月 28, 2006

ASM tutorial



JavaScript tutorial part III (taken from class notes)

JavaScript Comments- As mentioned before, one form of JavaScript comments are 2 forward slashes // with no spaces. That tells the JavaScript interpreter to ignore everything to the right on that line. You can also comment out a block of lines by using /* ... */
Anything you put between this syntax will be ignored. The */ marks the end of the comment. It is a container, just like we have seen before. you must use JavaScript comment syntax within the JavaScript code.

Numbers, Strings, and Variables

Numbers and Strings- Two of the most common types of data are numbers and strings (text).
Numbers are just as you know them. Some examples are 4, -3, and 12.456 We can use any rational number in a program. When you type numbers directly into your code they are refered to as numeric constants.

A string is any combination of characters, digits, spaces, (pretty much any key you can type). String constants, known as string literals, are typed within quotes (usually double quotes). Some examples are "cat", "25%", "Fred J Flinstone", or "98103".

Storing Information in a Variable

Constants are useful to programming, but they are limiting since a constant's value is always the same. What we need is something that we can change as a program executes: a variable. A variable is a memory location that has been given a name, or identifier. It is used to store data, such as things or numbers. A statement that creates a new variable is called a declaration. A declaration consists of the keyword var followed by a new identifier to serve as the name of the variable. Here is an example:

var student;
var age;

This first declaration creates a new variable named student. The second declaration creates a variable named age. These variables can now be used to store data.

A statement that stores information in a variable is called an assignment statement. It has the following form:

variableName=expression;

The equal sign ( = ) is called the assignment operator. Here is an example:

student="Arnold"; // notice the semicolon at the end of the statement
age= 20;

Assignment statements are read from right to left. The first statement assigns the string literal "Arnold" into the variable student (we'd say storing "Arnold" in student). The second statement assigns the constant 20 to the variable age. We are using 2 different kinds of data here: "Arnold" is string data: 20 is numeric data.

A variable declaration and assignment are sometimes combined into one statement like this:

var student = "Arnold";

However, note that a variable must be declared only one time, but may have a new value assigned to it any number of times. So, this type of combined declaration/assignment maybe used only the first time a value is assigned to a variable.

Realize that there is a HUGE difference between the variable student and the strring literal "student".

Syntax Rules for Variable Names
There are some rules you must follow when creating variable names.
-You cannot use a reserved word (or key word) as a variable name. Recall that reserve words are identifiers with special meaning in the JavaScript language.
-A variable name can contain letters, digits, the underscore ( _ ) and the dollar sign ( $ ). The name cannot start with a number. Usually they start with a letter.
-There can be no spaces in a variable name. If the name has several words in it, like milesPerGallon, you could right the name as shown here, using lowercase letters and uppercase letters to make it easier to read. Or you can use the underscore ( _ ) like this: miles_per_gallon.
-Make the variable names meaningful, a name like w does not tell the reader much, but the name width is much more meaningful.

The empty String

There is one special string literal you need to know about, the empty string or null string. This is noted by 2 quotes next to each other, with nothing in between. Most often we use double quotes; yet we'll see an empty string as single quotes in some situations. For example "" is the empty string. Input and output.

Getting Input from the User

Recall the confirm ( ) and prompt ( ) methods of the window object. The confirm method is passed a string value, and then displays that text in a popup window that has 2 buttons. for example the following code created this window.confirm("Select a button please");

The method returns the value true if the user clicks OK; it returns false if the user clicks on the cancel. true and false are keywords in JavaScript.

The prompt ( ) method has 2 string parameters. The first string is the text displayed in the pop-up window. The second string is the text displayed within the text field of the window. The user can always replace this information; it is a way of displaying a default value. If you don't want any text in the field you pass in the empty string. Here are 2 examples:

window.prompt ("Please enter your state", "WA");
//use a default state of Washington

window.prompt ("Please enter your name","");
// no default makes sense for a persons name

The method returns the text that is in the text field once the user clicks OK. (Remember that that the user can just leave the default value, or type in something else). If the user clicks the cancel button, the method returns the empty string.

These return values can be used to get input (information) from the user. This information can be stored in a variable. Once information has been stored in a variable, it may be used any number of times. Heres an example of storing the return value in a variable:

var name = window.prompt ("Please enter your name","");

or

var answer;
answer= window.confirm ("Select a button please");

the information in a variable may then be used by placing the variables name in an expression. When a variable is shown in an expression, the value of that variable used. For example, this code:

var student = "Arnold";
window.alert( student );

Shows an alert window with the word "Arnold" in it (the quote marks are not shown) because "Arnold" is the value of the variable student. Realize that this will always display "Arnold" because the variable student is holding a strring literal (or string constant).

Using the prompt window allows users to enter something different every time the program is run. For example:

var student = prompt ("Enter your name","");
document.write ("You entered " + student);

Just by looking at this code, we don't know what will be displayed! Whatever the user enters in the prompt window will be stored in the variable student. Then, the value of that variable is written to the web page using the write ( ) method. Remember the word student will not be displayed. See the echo and the windows method examples to see these illustrated.

Since the prompt ( ) method will always return a string, how do we get numbers into our programs? You will need to parse the return value from prompt ( ).

Providing Output to the User
There are several ways that we can provide output to the user:

document.write ( )

The document.write ( ) method can be used to show output on the web page (the method we will use most). Here is an example

document.write ("High there all! <br>);
There must be one string expression within the parentheses. Notice that you can put html tags within the string. These will in turn be interpreted by the browser and used properly.

Here is another example with a larger string expression:

var name = "Sally";
document.write ("Is your name <u>" + name + "< /u>? <br>");

The following would be displayed on the screen:
Is your name Sally?

You can put any html tags between the string. See the boxes and discount examples for some interesting results.

alert ( )
An alert window can be used to force the user to acknowledge the output shown in a seperate window. ((unless you have javascript diabled in your browser)) Promt and confirm windows also show output, but are really used primarily to get input.

Other options
Output may be shown in the windows status bar by assigning a value to window.status.
Output may be shown in the windows title bar by assigning a value to document.title (yes, the title is "owned" by the document, rather than the window).

0 Comments:

コメントを投稿

Links to this post:

リンクを作成

<< Home