JavaScript control flow and expressions

Source: Internet
Author: User

One, with statements

Using the WITH statement simplifies some types of javascript statements by reducing multiple references to one object to a reference, and references to properties and methods in the With block are treated as references to objects.

<Script>string= "The quick brown for jumps over the lazy dog" with(String) {document.write ("The string is" +length+ "characters<br/>") document.write ("In upper case it ' s" +touppercase ())}</Script>

In the above code, the JavaScript compiler recognizes that the length property and the toUpperCase method must be applied to an object, and the compiler considers them to be related to the string object specified in the WITH statement.

Ii. use of the OnError event

Events are behaviors that can be detected by JavaScript, and each element on a webpage has certain events that can be used to start JavaScript functions. The OnError event can redefine the action that is performed when an error is detected.

<Script>onerror=Errorhandlerdocument.writ ("Welcome to this website")functionErrorHandler (message,url,line) {out= "Sorry,an error was encountered.\n\n"; out+= "Error:" +message+ "\ n"; out+= "URL:" +URL+ "\ n"; out+= "Line :" + Line+ "\ n"; out+= "Click OK to continue.\n\n";    alert (out); return true;}</Script>
Iv. catching exceptions using the Try ... catch statement

The Try...catch statement is more standard than the onerror technique, which catches exceptions in the selected block of code, rather than the entire script. Of course, they do not catch syntax errors and need to be onerror to catch syntax errors.

<Scripttype= "Text/javascript">    Try    {        //Do somethings    }    Catch    {        //If you catch an error    }    finally    {        //The code in this block of statements is executed regardless of the error.    }</Script>
V. CONDITIONS and CIRCULAR statements

The conditions and loops of javsscript are basically consistent with C + +: If ... else, switch ... while and for loops, and break and continue that jump out of the loop are not produced much.

JavaScript control flow and expressions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.