Javascript Study Notes [3]-Process Control

Source: Internet
Author: User

After playing the game for a while, you can calm down and read the book. Today, you can see the process control and function basics. First, let's summarize the process control and then summarize the function control, tomorrow we should be able to access JavaScript object-oriented programming. I mainly look at the Javascript authoritative guide-edited by Zhang Yafei-Tsinghua University Press; I also referred to the Reference Manual for JavaScript core objects-han yanfeng-people's post and telecommunications press, and Baidu's own online, I will attach links to some good blog posts and respect those original authors. Now start:

3. Process Control

Some simple process control, such as if .... else if... else statements, switch statements, while, And do while statements are similar in almost all languages. Just take a look at them and leave it alone. Here are some interesting process control statements:

1) For... in Loop

Syntax: For (variable in object)

Statement;

For... the in statement traverses the attributes of objects in the prototype chain of an object. In some cases, for example, object methods, Host Object Attributes, static members, it's messy. Let's stay and know what the prototype is. Let's link the knowledge and try again.

<SCRIPT> VaR Arr = New Array ("this", "is", "A", "test" );  For ( VaR I In  ARR) {document. Write (ARR [I] + "\ T" );} </SCRIPT> //  Output this is a test //  This example is quite good.  VaR Myobject = {hisname: "JavaScript", age: 11, belonging: "ECAM" };  For ( VaR Prop In  Myobject) {document. Write ( "Myobject." + Prop + "=" + myobject [prop] + "<br/>" );}  //  Output:  //  Myobject. hisname = Javascript  // Myobject. Age = 11  //  Myobject. Belonging = ECMA 

2) For each... in Loop

Syntax: For each (variableiterant in object ){

Statement;

}

For each... In traverses the object property value, not the property name.

//Or this exampleVaRMyobject = {hisname: "JavaScript", age: 11, belonging: "ECAM"};For(VaRPropInMyobject) {document. Write (item+ "<Br/>"):}//Output://Javascript//11//ECMA

3) label statement

Provides an identifier for the statement to directly jump to the row specified by the identifier to start execution. It can only be used by break and continue statements to indicate the statement from break and continue, actually, it is rarely used. This is the first time I have seen it. Below is a good example

<SCRIPT type = "text/JavaScript"> Outerloop:  For ( VaR I = 0; I <10; I ++ ) {Innerloop:  For ( VaR J = 0; j <10; j ++ ){  If (J> 3 ){ Break  ;}  If (I = 2 ){  Break  Innerloop ;}  If (I = 4 ){  Break  Outerloop;} document. Write ( "I =" + I + "J =" + J + "<br>" );}} </SCRIPT>

4) Let Statement (newly added in Javascript 1.7)

It's quite new. It's very short in the book, and there are very few online materials. I only know that let statements allow the use of identifiers to mark a statement.

5) yield statement

This is quite useful. Yield statements can generate an output value in a loop statement and return it. The function that contains the yield statement is called a generator. Generally, the iterator is used to traverse the generator.

// This yield statement is only visible in the Firefox browser and not displayed in other browsers.
<SCRIPT type = "application/JavaScript; version = 1.7"> Function Generator (){ VaR I = 0 ; While (I <10){ // Generate output Yield I; I ++ ;}} // Get the generator VaR G = Generator (); // Iteration For ( VaR J = 0; j <10; j ++ ) {Document. Write (G. Next () + "<Br>" );} </SCRIPT> // Do not use the generator <SCRIPT> Function Writevalue (PARAM) {document. Write (Param + "<Br/>" );} Function Generator (){ VaR I = 0 ; While (I <10 ){ // Generate output Writevalue (I); I ++ ;}} Generator (); </SCRIPT>

6) with statement

It is used to conveniently reference the methods and attributes of a specific object.

 
VaRRightnow =NewDate ();With(Rightnow) {document. Write (getday ()+ "<Br>"); Document. Write (getmonth ()+ 1 + "<br>"); Document. Write (getfullyear ()+ "<Br>");}//That is, the rightnow object is written less.

 

All of the above are personal originals. Please refer to the original link when you reprint it:Http://www.cnblogs.com/tonylp

 

Related Article

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.