Home  >  Article  >  Web Front-end  >  Detailed explanation of process statements in JavaScript

Detailed explanation of process statements in JavaScript

青灯夜游
青灯夜游forward
2018-10-12 15:45:542494browse

This article brings you a detailed explanation of the process statements in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Overview of the opening chapter

This lecture mainly explains JavaScript process statements. Its general content includes the following:

Among them, the commonly used if, while, do ..while, for will not be discussed in this article. The focus will be on for..in.., label, break and continue, whth, switch and other statements

二contentarea

(1) Commonly used statements

Since the following statements are relatively common, this article will not discuss them

1. Conditional statement

if statement

2. Loop statement

while statement, do.while statement, for statement

(2) for..in.. statement

1. Definition

for...in... is an iterative statement used to enumerate objects attribute, its syntax is defined as:

for (propName in expression) statement

Based on the principle of "If you can use local variables, don't use global variables" in JavaScript development, it is recommended to define the propName attribute as a local variable, as follows:

for (var propName in expression) statement

2. Notes

(1)for....in is used to enumerate object properties, not to enumerate object property values.

Example 1:

In the following example, for..in.. outputs the array index (that is, the array attribute), not the array index value.

var i = 5;
    var arr = new Array();

    for (var n = 0; n < i; n++) {
        arr[n] = i;
    }

    for (var propName in arr) {
        alert(propName);// 0,1,2,3,4
    }

Example 2:

In the following example, for..in.. outputs the attributes (name, age, address) of the object userInfo, but not the attribute values ​​(Alan_beijing,38,china -shanghai)

var userInfo = { name: &#39;Alan_beijing&#39;, age: 38, address: &#39;china-shanghai&#39; };
for (var property in userInfo) {
alert(property);//name,age,address5     
}

(2)for..in..enumeration properties, there is no definite order, different browsers will have differences.

(3) Before the ECMAScript5 version, if the value of the iterated object variable is null or undefined, the for statement will throw an error. After ECMAScript5, this situation will not throw an error, but the loop body will not be executed.

(3) label

1. Definition

In JavaScript, the label statement represents a label statement, which is usually used with a loop statement to represent a loop statement. Jump to the specified location.

1 label:statement

Example 1:

The following code contains a label statement outermost, whose content is two nested loop bodies. When the loop body is executed to 1==5 and j==5 , the break statement will jump to the outermost statement to continue execution.

var num = 0;
    outermost:
    for (var i = 0; i < 10; i++) {
        for (var j = 0; j < 10; j++) {
            if (i == 5 && j == 5) {
                break outermost;
            }
            num++
        }
    }
    alert(num);//55

(4) break and continue

1. Definition

Both break and continue are expressed in the loop body and exit the loop according to specific conditions body, but there is a difference between the two. break means exiting the entire loop body, and continue means exiting the loop body that meets the conditions.

Example 1:

The following code will exit the entire loop when i=5 is executed.

var num = 1;
    for (var i = 1; i < 10; i++) {
        if (i % 5 == 0) {
            break;
        }
        num++;
    }
    alert(num);//5

Example 2:

The following code, when executing i=5, exits this loop, then returns to the beginning of the for statement and continues execution.

var num = 1;
    for (var i = 1; i < 10; i++) {
        if (i % 5 == 0) {
            continue;
        }
        num++;
    }
    alert(num);//9

2. Notes

(1) When break and continue jump out of the loop body, it means that only the immediate loop body will be jumped out, not other loop bodies except the direct loop body.

Example 1:

In the following example, break only jumps out of the direct loop body

##Example 2:

The following example , continue only jumps out of the direct loop body

var num = 0;
    for (var i = 0; i < 10; i++) {
        for (var j = 0; j < 10; j++) {
            if (i == 5 && j == 5) {
                continue;
            }
            num++
        }
    }
    alert(num);//99

2.break and continue are generally used in conjunction with label statements to indicate jumping to the specified location

Example:

The following code, when executed When i=5 && j==5, jump to the label statement outermost and continue execution. It should be noted here that JavaScript does not have a block-level scope, so the variable i can be accessed outside the for statement

var num = 0;
    outermost:
    for (var i = 0; i < 10; i++) {
        for (var j = 0; j < 10; j++) {
            if (i == 5 && j == 5) {
                break outermost;
            }
            num++
        }
    }

    alert(num);//55

(5) with

1. Definition

The with statement sets the code scope to a specific object. Its main purpose is to simplify writing the same object multiple times and improve reuse.

1 with (expression) statement
Example:

The following code defines a function to obtain user information, creates a new person object in the function body, and defines two attributes (name and address), and then uses the person object with stand up.

function GetUserInfo() {
        var person = new Object();
        person.name = "Alan_beijing";
        person.address = "China-shanghai";
        with (person) {
            return name +","+ address;
        }
    }
    alert(GetUserInfo());//Alan_beijing,China-shanghai

2. Notes

(1) In JavaScript development, use the with statement with caution. There are two main reasons: one is that the with statement affects performance; the other is that the with statement is strictly prohibited In mode, an error will occur

(2)The with statement encloses the public object, thereby improving code simplicity and code reusability

(3)When searching for variables in the body of the with statement , first check whether the variable you are looking for exists in the body of with. If it does not exist, then check whether the variable enclosed with with has the attribute you are looking for.

The following examples better reflect this principle:

function GetUserInfo() {
        var person = new Object();
        person.name = "Alan_beijing";
        person.address = "China-shanghai";
        person.age = 35;
        with (person) {
            var sex = "男";
            var age = 40;
            return name + "," + sex + "," + age +","+ address;
        }
    }
    alert(GetUserInfo());//Alan_beijing,男,40,China-shanghai

(6) switch

1. Define the

switch statement It is what we usually call a switch statement. It is very suitable for multi-condition situations.

switch (expression) {
        case value: statement
            break;
        case value: statement
            break;
        default:statement
    }

Example:

如下代码,根据城市名称,查询城市类别

//根据不同城市,判断其属于几线城市
    function CityType(address) {
        switch (address) {
            case "Shanghai": alert("中国一线城市");
                break;
            case "Shenzhen": alert("中国一线城市");
                break;
            case "Beijing": alert("中国一线城市");
                break;
            default: alert("中国非一线城市");
        }
    }

    CityType("Shenzhen");//中国一线城市

2 注意点

(1)switch本质与if是一样的,都是解决多条件多分支问题;

(2)使用switch语句的真正目的是避免使用过多的if..else if ...else....语句;

 三  总结

本篇文章主要结合代码介绍了JavaScript的流程语句及其使用,重点结束了with,switch,for...in..,label,break和continue等语句,需要注意的是,在JavaScript中,流程语句都没有块级作用域,至于什么是块级作用域,将在接下来的文章中与大家分享。

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关教程请访问JavaScript视频教程

相关推荐:

JavaScript图文教程

JavaScript在线手册

The above is the detailed content of Detailed explanation of process statements in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete