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

Here is a simple example for the usage of the continue statement:

var sum = 0 // will hold the sum of the odd numbers

for (var i = 1; i <= 10; ++i) {
  if (i % 2 == 0) // 1: if i is even
  continue  // 2: go to the top of the loop
  sum += i // 3: add i to sum (i is always odd!)
}

This example adds up every odd integer from 1 to 10. The easiest way to do that is to increase the counter variable by 2 after each pass through the loop, but we wanted to feature the continue statement.

The loop in the preceding script segment is quite simple. It executes while the value of i is less than or equal to 10. Therefore, the loop executes 10 times, once for each integer value of i. The first line checks if i is an even number, using a simple modulo operation. If the number is even (that is, i % 2 evaluates to 0), the expression i % 2 == 0 evaluates to true. In that case, the continue statement on the second line is executed, causing the loop to start from the top, at the update expression. The following diagram illustrates the flow of the loop:


Figure 8-6.  The flow of the preceding script segment.

Here is another example using the continue statement:

for (i = 1; i <= 10; ++i) {
   if (i == 5)
  continue
   document.write(i + " ")
}

The output is:

1 2 3 4 6 7 8 9 10

Notice that the 5 is missing, because when i is equal to 5 the loop is sent to the beginning, incrementing i to 6. The continue statement is located before the document.write() statement, so the document.write() method is never invoked when i is 5.

with Statement

with (object)
  statement

Writing formal addresses to access properties or invoke methods of the same object is often annoying. The with statement establishes a default object for a set of statements. Within the specified set of statements, any property references that do not specify an object are assumed to be governed by the default object defined by the with statement. The same applies to methods belonging to that object.

The with statement is useful only when referring to several properties and methods of the same object, so it should always be followed by a command block:

with (object) {
  statements
}

We haven’t discussed user-defined objects, so take a look at an example where the default object is a built-in one:

with (document) {
   write("Notice that the document object is default")
   write("<BR>")
   write("The current background color is " + bgColor)
   // bgColor is normally document.bgColor
}

The output is:

Notice that the document object is default
The current background color is #c0c0c0

You will find this statement useful after learning about built-in and user-defined objects. For now, just remember what it does, and use it with your existing vocabulary if you wish.

You may recall from the beginning of the chapter that the default object throughout the script is the window object. You can invoke its methods anywhere without literally specifying that they belong to the current window object. For example:

alert("STOP")
confirm("OK to proceed?")
prompt("How many cookies would you like?", 12)

An alternative syntax specifies the window object as in window.alert("STOP"). Generally speaking, you can refer to any script as if it were placed in a with statement’s command block, where the argument specifying the default object is window.

<SCRIPT LANGUAGE="JavaScript">
<!-- hide code

with (window) {

}

// end hiding code -->
</SCRIPT>

You can also implement nested with statements. In such cases, several default objects exist simultaneously. The scope (where each one exists) is very simple. Inside a command block associated with a with statement, you can refer to the properties and methods of the default object, without specifying their belonging. The following example demonstrates the scope concept:

with object1) {

 // object1 -- default

 with (object2) {

   // object1, object2 -- default
 }

 // object1 -- default

 with object3) {

   // object1, object3 -- default

   with (object4) {

 // object1, object3, object4 -- default

   }

   // object1, object3 -- default

 }

 // object1 -- default

}

// no default objects (except window)

Summary

In this chapter you have learned how to take advantage of the computer’s capacity for repetition. At first, we looked at the most simple ways to interact with the user, via JavaScript methods that generate dialog boxes. You should now be familiar with the if-else structure, as well as the basic looping statements. We discussed the continue and break statements that enable flexible loops. By now, you should also know how to use the for…in loop and the with statement. These are basic object-oriented programming tools.

In addition to all standard control structures in JavaScript, we discussed some statements that are implemented in C++ and Perl but are not supported by JavaScript yet.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us