In-depth understanding of several key statements of ECMAScript and several key statements of ecmascript

Source: Internet
Author: User

In-depth understanding of several key statements of ECMAScript and several key statements of ecmascript

Preface

In this chapter, we will talk about several key statements in ECMAScript, such as switch, for-in, and label, to deepen our understanding and understanding of them. First, let's start with the most common one.

While and

While and for are common statements, either in JavaScript, C or other programming languages. In addition, we often use for in programming. for is more flexible and simple, so some people may have the following misunderstanding:

For is more powerful than while, and can do some things that while cannot do.

In fact, if we think about the syntax application of the while and for statements, we will find that:

A while LOOP cannot do either.

This is because the for loop only integrates the Code related to the while loop. In fact, it is easier to use the while LOOP than to use the for loop. It also has its own advantages and disadvantages.

Let's take a look at the code for another point of loop:

for(i=0;i<5;i++){  console.log(i);}console.log(i);

Print I out of the loop, and the print output is 5.

As you can see, variables defined inside the loop can also be accessed externally. In some languages, for example, C, braces define block-level scopes, but ECMAScript does not have block-level scopes, therefore, variables defined inside the loop can be accessed externally.

Switch statement

In other programming languages such as C, switch statements can only use numbers, while in ECMAScript, switch statements can use any data type, such as strings and objects.

Here, we need to note that the fully equal operator (=) is used when the switch statement is compared, so '10' and 10 are not equal, because during the full comparison, no type conversion occurs.

For-in statement

The for-in statement is an accurate iterative statement that can be used to traverse the attributes of an object. Of course, it can also iterate the attributes of an array. The following are examples:

For-in traversal object

• Window

Traverse a special object window first:

for(var i in window){  console.log(i);}

A long and long attribute list will be printed. You can view the list on your own.

• Custom object

Traverse custom objects

var o={prop1:'value1', prop2:'value2', prop3:'value3'};for (var i in o){  console.log(i);}

Print prop1 prop2 prop3.

• Array

Traverse Arrays

var array1=[1,2,3,4];for(var i in array){  console.log(i);}

Print the output 1 2 3 4.

With statement

The with statement can be used to limit the scope, that is, the Code scope can be set to a specific object. As follows:

var hostname=location.hostname;var url=location.href;

The two statements obtain the hostname and url respectively. Because they share location (attributes under the same object), we can restrict the scope to location, that is, associate the location object with the with statement. As follows:

with(location){    var hostname=hostname;  var url=href;}

Note that the use of with statements in strict mode may cause syntax errors. Using with statements in large quantities may lead to performance degradation and may also cause some difficulties in debugging, therefore, when developing applications, especially large applications, we do not recommend using the with statement.

Label statement

The label statement is used to add a label in the Code so that it can be used later. In general, the label-adding statement should be used with loop statements such as for loop.

Its syntax is:

1. First, give a piece of basic code:

var num=0;for(var i=0;i<10;i++){  for(var j=0;j<10;j++){    if(i==5&&j==5){      break;    }    num++;  }}console.log(num);

Note: The break jumps out of the internal for loop, and the remaining 5 cycles of j are not executed, so the printed result is 95.

2. Replace break with continue:

var num=0;for(var i=0;i<10;i++){  for(var j=0;j<10;j++){    if(i==5&&j==5){      break;    }    num++;  }}console.log(num);

Note: continue jumps out of this loop, that is, it jumps out of this loop in the internal for loop, so the printed result is 99.

3. Add a label named outer to view the printed result:

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

Note: After the tag is added, the break is used to jump to the tag outer, that is, the program jumps out of the External Loop, that is, the program stops executing when it reaches I = 5 and j = 5, so the printed result is 55.

4. Let's change to continue to see:

var num=0;outer:for(var i=0;i<10;i++){  for(var j=0;j<10;j++){    if(i==5&&j==5){      continue outer;    }    num++;  }}console.log(num);

Note: This time we use continue, so when the program runs to I = 5 and j = 5, it does not jump out of the External Loop, but just jumps out of the internal loop, that is, the remaining five times are not executed, so the printed result is 95.

It seems a little confusing to put them together, so it will be much better to understand more.

Summary

We learn one thing not for learning, but for learning. To put it bluntly, we want to learn it for work. Therefore, simply understanding the above is not the most important thing, not for remembering, it is just what we hope to remember for use and be proficient in practice. At the same time, taking notes is a good habit, so it is better to have a good memory than a bad pen. If you are doing it, I hope you will stick to it.

The above several key statements for understanding ECMAScript are all the content shared by the editor. I hope to give you a reference and support for the customer's house.

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.