A deep understanding of ECMAScript's key statements _ basics

Source: Internet
Author: User
Tags list of attributes

It's written in front.

In this chapter we talk about several key statements in ECMAScript, such as Switch, for-in, label and so on, deepen our understanding of them. First, from the most common.

While and for

While and for are common statements, whether in JavaScript or C or in other programming languages. And in programming we are more commonly used for,for is more flexible and simpler, so some people may have such a misunderstanding:

For is more powerful than while, can do some things while do not.

In fact, we think a little bit about the syntax application of the while and for statements, and we find that:

The For loop that the while loop cannot do is also not done.

This is because the for loop only centralizes the code associated with the while loop, but sometimes it's simpler to use a while loop than a for loop. It is also the pros and cons of each of their respective roles.

Another point about loops is that we'll look at a piece of code:

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

Printing I outside of the loop, the printout is 5.

As you can see, the variables defined within the loop are also accessible externally. In some languages such as C, curly braces define block-level scopes, but there is no block-level scope in ECMAScript, so variables defined within the loop are also accessible externally.

Switch statement

In other programming languages such as c,switch statements can only use numbers, and in ECMAScript, a switch statement can use any data type, such as a string, an object can be.

Here's one thing to note: The strict equality operator used when the switch statement is compared, that is, = = = ' 10 ' and 10 are not equal, because type conversions do not occur when congruent comparisons are made.

For-in statement

A for-in statement is an iterative statement of accuracy that can be used to iterate through the attributes of an object, and, of course, to iterate over the attributes of a group. The following examples illustrate:

For-in Traversal Object

window

First walk through a special object window:

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

Will print out a long and long list of attributes, you can see for yourselves, not to list in detail.

• Custom Objects

Traversing a custom object

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

Print out Prop1 prop2 prop3.

• array

Traversing an array

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

Print Output 1 2 3 4.

With statement

The WITH statement can be used to restrict the scope, that is, the scope of the code can be set to a specific object. As follows:

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

These two sentences get hostname and URLs separately because they share the location (the properties under the same object), so we can limit the scope to location, which is to associate the location object with the WITH statement. As follows:

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

It is important to note that using the WITH statement in strict mode can have syntax errors, and that a large number of with statements can cause performance degradation while also causing some difficulty in debugging, so it is not recommended to use the WITH statement when developing applications, especially when developing large applications.

Label statement

A label statement is used to label the code so that it can be used later. In general, tagged statements are used in conjunction with loop statements such as for loops.

Its syntax is:

1, first give a piece of basic code:

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

Description: Break jumps out of the inside for Loop, J the remaining 5 cycles are no longer executed, so the result of printing is 95.

2, next we will break to replace continue:

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

Description: Continue jumped out of this cycle, that is, jumped out of the internal for loop this time round, so the result of printing is 99.

3. Next we add a label label labeled outer to see the printed result separately:

var num=0;
Outer: For
(var i=0;i<10;i++) {to
  (var j=0;j<10;j++) {
    if (i==5&&j==5)
      {break outer;
    }
    num++
  }
}
Console.log (num);

Note: Add the label, with the break jump to the label outer, that is, the program jumped out of the loop, that program execution to the i=5 and j=5 stop execution, so the result of printing is 55.

4, we will 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);

Description: This time with the continue, so when the program to i=5 and J=5 did not jump out of the outer loop, but only jumped out of the inner loop, that the remaining 5 times do not execute, so the result of printing is 95.

Put together these few appear to be slightly confused, more understanding understanding will be much better.

Summary

We learn a thing is not to learn and learn, is to use and learn, plainly is to work and learn, so simply understand the above things are not the most important, not for the record, is for the use and remember, can be skilled in practice is what we hope. At the same time, taking notes is a good habit, good memory is not as bad as written, if you are doing, then hope you stick to it.

The above in-depth understanding of the ECMAScript several key statements are small parts to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.