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

Because the function does not send direct output to the screen, you must use a document.write() statement to print the returned value. Notice that the arguments handed to the function in this case are similar: Math and "Math". The first argument holds the object reference itself, and the second one holds the name of the object, which is a simple string. Once inside the function, the object is referenced as obj, so there is no way to extract its original name automatically.

The getProps() function implements the array notation to access properties. We use this notation because the property’s name is not a literal but rather a data structure holding a string value. The dot syntax can only be used when the name is in the form of a literal. This rule also applies to methods. Consider the following expressions:

var a = "write"

document.write("Test 1 <BR>")
document.a("Test 2 <BR>") // error
document["write"]("Test 3 <BR>")
document[a]("Test 4 <BR>")

The second test generates an error, but the rest print the output as we expected, because the dot syntax accepts only literals as its specifications.

break Statement

  break

Normally, a loop executes until the terminating condition is met, but you might want it to terminate earlier if it meets some other condition. The break statement does just that; it terminates the loop’s execution. In C++, the break statement also terminates switch statements, but JavaScript does not support this statement yet.

The following script segment demonstrates the break statement:

for (var i = 0; i < 100; i++) {
  document.write(i + " ")
  if (i  == 50) // additional terminating condition
 break // break out of the loop
}

The output of this loop is:

0 1 2 3 4 5 6 7 8 9 . . . 41 42 43 44 45 46 47 48 49 50

As you can see, the loop terminates when i is 50, not when i is equal to 100. That is, it terminates before the condition i < 100 evaluated to false. When i is equal to 50, the condition i == 50 returns true, and the break statement executes, causing the loop to terminate. The break statement lets you place additional terminating conditions at various locations in a loop’s command block.

Note that the break statement terminates all loop statements.

Although goto statements are disallowed in modern programming, it is sometimes difficult to perform certain actions without the help of this statement. Unfortunately, JavaScript does not provide that tool, so you must find workarounds even for sophisticated structures. Consider the following control structure:

for (i = 1; i <= 10; i++) {
   for (j = 1; j <= 10; j++) {
  for (k = 1; k <= 10; k++) {
 for (l = 1; l <= 10; l++) {

 }
  }
   }
}

/* spot */

Suppose you want to terminate the execution of the entire control structure when l, the inner loop’s counter, is equal to 5. The problem is that a break statement only terminates the loop in which you place it, not other loops that govern it. Using a goto statement in C++, you could have leaped directly to the desired spot. However, JavaScript does not support this statement, so you must find a simple workaround. The following script presents a remedy using an additional variable:

var continueLooping = true
for (i = 1; i <= 10; i++) {
   for (j = 1; j <= 10; j++) {
  for (k = 1; k <= 10; k++) {
 for (l = 1; l <= 10; l++) {
if (l == 5) {
   continueLooping = false
   break
}
if (continueLooping == false) break
 }
 if (continueLooping == false) break
  }
  if (continueLooping == false) break
   }
   if (continueLooping == false) break
}

For each pass through the loop, continueLooping is evaluated. If it is false, the current loop is terminated. continueLooping is assigned false in the inner loop when the condition l == 5 is true, and then each loop terminates in turn. Note that it is extremely important to place the if (continueLooping == false) break statements immediately after the closing brace of the nested loop, so that the loop terminates when the nested loop terminates, and no other statements are executed in between.

The break statement in JavaScript is very important, because it is used to create alternatives to loop statements that do not exist in JavaScript. An important loop type in C++ and Perl is do…while. It executes a statement as long as the condition is true. Unlike the regular while loop, the condition is evaluated at the end of the loop, so the loop executes once without having tested prior to that. However, JavaScript does not feature such a loop. Once again, it will probably be implemented in a future release of the language. For now, you must come up with a simple alternative using the existing statements. Take a look at the following structure:

while (1) {
  statements
  if (condition)
break
}

Notice that the preceding script segment is equivalent to the repeat…until structure in Pascal, and the do…until in Perl, but it doesn’t match the do…while loop in C++ and Perl. You just have to negate the condition in the following form:

while (1) {
 statements
 if (!condition)
  break
}

continue Statement

continue

The continue keyword stops the current iteration of a loop, and continues the execution at the top of the loop. The loop continues to execute until the terminating condition is achieved. The “top” of the loop is:

  • The update expression in for loops, such as ++i
  • The condition expression in while loops
Previous Table of Contents Next


With any suggestions or questions please feel free to contact us