Online Documentation Server
 ÏÎÈÑÊ
ods.com.ua Web
 ÊÀÒÅÃÎÐÈÈ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ÏÎÄÏÈÑÊÀ

 Î ÊÎÏÈÐÀÉÒÀÕ
Âñÿ ïðåäîñòàâëåííàÿ íà ýòîì ñåðâåðå èíôîðìàöèÿ ñîáðàíà íàìè èç ðàçíûõ èñòî÷íèêîâ. Åñëè Âàì êàæåòñÿ, ÷òî ïóáëèêàöèÿ êàêèõ-òî äîêóìåíòîâ íàðóøàåò ÷üè-ëèáî àâòîðñêèå ïðàâà, ñîîáùèòå íàì îá ýòîì.




Previous Table of Contents Next

Programming Style

As you read this book, you will see that we use a uniform coding scheme in all scripts. That is, we always use the same indentation practice and the same formation. For example, you will find that we always put the opening semicolon of a loop on the same line as the loop’s keyword. Take a look at the following script segment:

for (var num = 0; num < 10; ++num) {
   [JavaScript statements]
}

Some JavaScript scripters prefer to use a different style:

for (var num = 0; num < 10; ++num)
{
   [JavaScript statements]
}

The same applies to comments. Consider the following comment:

/* Copyright (c) 1997. All rights reserved.
Permission given to use this script provided
that this notice is left as is. */

Instead we would normally use single-line comments, because we believe that they emphasize the comment:

// Copyright (c) 1997. All rights reserved.
// Permission given to use this script provided
// that this notice is left as is.

You should feel free to choose the style that you like most, because there is no such thing as an incorrect choice. However, if you have never programmed before, it is recommended that you use the same coding scheme we use. If you are an experienced programmer, you may prefer to stick with your habits. For example, most C and C++ programmers choose to place a semicolon at the end of each statement. Although not required, JavaScript works fine with semicolons, just as C++ does.

Reading Examples

A picture is worth a thousand words, and so is a good example. However, if you do not read an example properly, it isn’t worth anything. When you encounter an example of a script segment, read through it and try to find what it does. Even if you cannot understand how everything is being accomplished, try to get the main idea. First, try to find where the execution starts. For example, if a JavaScript script includes a global function call, it is surely the first statement to be executed. Follow the natural flow dictated by the script. The most important part of a script is its comments, so you should read every one of them. It is probably more important that you read the comments first, rather than reading the statements without the comments.

The second stage in reading an example is reading the explanations that usually follow the script. Every script is provided with complete documentation, so you should understand every single statement. If you try hard and simply can’t figure something out, don’t hesitate to run the script under your browser. As a matter of fact, since it is difficult to learn computer programming without using a computer, it is recommended that you try executing all scripts.

Encouragement for Nonprogrammers

Learning JavaScript is much easier than mastering a full programming language such as C or Java. Using JavaScript, you can write small scripts which you can immediately execute and debug. The JavaScript interpreter gives you immediate feedback on your script correctness, and until you learn a compiled language, you’ll never appreciate this powerful capability. Implementing a similar task in C or Java is probably more difficult.

The advantage of computers is that they do exactly what you tell them to do. The disadvantage of computers, on the other hand, is that they do exactly what you tell them to do. Think about this axiom and internalize it, because every programmer needs to come to terms with it before he or she can become successful. Programmers become frustrated usually because of two types of errors. The first type is syntax errors. The computer understands sentences that are written in the exact syntax as it is specified in this book (unless a mistake has been made). If you misspell a command or a function name, the computer won’t understand it and will return an error message. The more difficult error to find is when the computer does not complain but does not do what you want it to do. This is one of the laws of programming—the computer does what you tell it to do, not what you want it to do. This kind of error, also called a semantical error, is usually found by careful debugging, which is explained later in the book. Another method to find semantical errors is letting someone else look over your shoulder and read your script. Another person will sometimes catch an error that is overlooked by the original author.

Experience also shows that a wrong assumption lies at the root of every difficulty in finding an error. Once an assumption is made, the programmer never tests its validity and, instead, is looking for the source of the problem elsewhere. It is a good idea to always repeat to yourself which assumptions you are relying on and validate each one of them. It is recommended you discuss the problem with a friend. A programmer may find an annoying bug just by explaining the program and its assumptions to someone else.

Warning for Programmers

Some people say programming experience in a procedural language such as C or Pascal may almost be a disadvantage to learning JavaScript. Although this judgment is controversial, there is indeed a big difference between them. JavaScript’s object-oriented programming is very different from the procedural C language. The difference is classically demonstrated by mouse clicks. In a procedural language, the programmer is responsible for the screen appearance and everything that happens thereafter. Imagine a screen display of the classical Tetris game. The user has to program the blocks, the container, the score dial, etc. When the user clicks on a button (the Pause button for example), the program examines the coordinates of the click and compares those coordinates against all button coordinates on the screen (the programmer placed these buttons and therefore he or she stored the coordinates in the beginning of the program). Program execution then goes to the proper procedure and carries out the instructions associated with that button (pausing the Tetris game in our example).

Object-oriented programming is 180 degrees from the above. The Tetris Pause button is constructed as an object. An object is defined by its properties and by a set of operations defined to work with it. The Pause button’s properties include its label, size, color, and icon. Operations defined for it may include the color change upon clicking and sending an instruction to another object to carry out the pause action. If a user clicks in a text entry field, the browser sends a message to the field to take care of the user actions as long as the insertion point is within the text field. An operation defined for a text field is written as a script segment. There is a script to handle typing in the field or clicking outside the field.

Don’t be confused. JavaScript programming involves procedural construction as well. You will write a sequence of instructions in your script that will be in order. But when you work with JavaScript objects like form, radio button, text field, and image map, you will use their object properties and methods. Once you have defined the properties of the radio button, you won’t have to worry about it any more. It will take care of itself and that’s the beauty of object-oriented programming.

For those C programmers among you, you should feel very comfortable. Many language constructs were designed a la C style: Operator symbols, conditional structures, and repeat loops are almost identical. Data typing is looser in JavaScript. Data is either a number (integer or floating), a string, a Boolean, an object, or null. Converting from one type to the other is straightforward. Since you are familiar with the language syntax, invest your time in the semantics, notably in object hierarchy which is not supported by C. Also, don’t forget to master HTML.

Let the Show Begin

If you haven’t got cold feet by now and are ready to jump in the water, then flip the page to the next chapter. Make sure you have Netscape version 3.0x and up or Microsoft Internet Explorer version 3.0 and up, as well as a text editor for the HTML documents. You are to embark on a new path that will take you to new adventures!

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us