金曜日, 1月 27, 2006

JavaScript objects cont'

Some Blogworthy news before I continue to bore the world about JavaScript.

-Important news from Intel, 45nm Chips. Now thats small!

-Favorite Schneier post of the day, voting machine corruption continues.

-Don't see the problem please explain X-) lets blanket the earth with freedom, or at least free wireless!

-Blackberry foo, according to MacWorld, hearing set on possible blackout!

-What is going on with these ridiculus patents? Is Cingular going to try and patent !@#$%^&*() next?

OK, back to it-- in the last post I finished in the middle of explaining objects. (members)

--Two objects built into JavaScript that we will begin using right away are the window object and the document object. The following shows some (not all) of the properties and methods of these objects.

Object .........Window
properties- defaultStatus
Holds a string value that is displayed in the window's status bar as long as no other event overrides the default.
properties- document
Holds a reference to a document object --each window has an associated document.
properties- status
Holds the string value that actually appears in the windows status bar.
Setting the value of this property can immediately change what appears in the status bar.
document
title Holds a string value that is displayed in the title bar of the window that is showing the document.

Methods- alert(string)
Displays a dialog box (pop-up window) that shows the user a message and must be dismissed before the script can continue executing. The method has one string parameter (a string being just a sequence of characters -- a literal string is surrounded by quotes. A parameter is a piece of information required by a method. The value actually used in a method call is named argument)to specify the text shown in the alert box. The parameter type is listed within the parentheses.
Methods- confirm(string)
Like an alert box, but it provides the user with a choice of 2 buttons to click. This method returns a value that indicates which button was clicked by the user-- true if the ok button was clicked, false otherwise. (Note: true and false are not string values-- they are special values we will cover later.)
Methods- prompt (string1, string2)
Also displays a box that allows user input. It takes two string parameters and displays string one as the text in the pop-up window, like an alert box. It also provides the user with a place (called a field) to enter text as a response. This field will be initialized with string2. This method returns a string value that contains the text entered by the user.
document
write(string) Displays information in the document. The method requires a string parameter that contains the text to display to the web page, and possibly contains html code that will be used to format the web page.

Notice that methods always have parentheses (sometimes with parameters, sometimes empty).
Properties never have parentheses. This is a quick way to know which one you are looking at in JavaScript code.

Using Objects and their members
In order to access the property of an object, you must use the following form objectName.propertyName for example: window.defaultStatus is a reference to "the defaultStatus property of the window object", and window.document.title means "the title property of the document object, which is a property of the window object." The 'dot' operator (.) indicates that you want to 'reach into' the object and get at one of its members. Notice there are no spaces in this syntax.

You can assign data into a property, just as you assign a value in an html attribute, by using the assignment operator =. The data to be stored is on the right hand side; the place to store the data is on the lefthand side. For example to assign a new string to the defaultStatus property of the window, youd use the following syntax:

window.defaultStatus = "This is my page.";

To use this method (or call a method), the method name must always be followed by a set of parentheses. A method might require other information to be used properly. For example the alert method must be given a string in order to do its job. We say that a string requirement is a parameter. The actual data that is sent (or passed) to the method when the method is called is refered to as the actual argument, or just argument. In fact, more than one argument may be used , in which case they are seperated by comas ( , ) within the parentheses. However, the parentheses are required even if no arguments are used.

Here is an example. Recall that the write method of the document object requires a string parameter. So to use this method (or to call this method), we must give it a string argument, as you will see below:

window.document.write ("Hello world!");

This is a call to the right ( ) method of the document object. One argument is passed in: a string literal that reads "Hello world!".

Syntax Rules and Other Terminology
JavaScript statements must follow strict "syntax" rules -- rules that govern the way a statement is constructed and the punctuation used in the statement. Here is a partial list of syntax rules and definitions of some JavaScript terminology:

-JavaScript is case sensitive. Upper- and Lower-case letters are not interchangeable in JavaScript statements.
-The basic unit of JavaScript code is the statement. Each statement is like a seperate instruction. Normally we list only one statement per line of code, and the order in which statements are listed is very important.
-Every JavaScript statement ends with a semicolon ( ; )
-An identifier is the technical name for a word that is used for some purpose in JavaScript. Examples of identifiers we have seen so far include object names (like a window or document), property names (like title or status), and method names like prompt.
-A keyword or reverse word is an identifier that has special meaning in JavaScript. It cannot be used for any other purpose.
-The window object is the default object. Therefore any of its properties or methods may be used without specifying that it comes from the window object. For example we could rewrite the line of code above as

document.write("Hello world!);

0 Comments:

コメントを投稿

Links to this post:

リンクを作成

<< Home