Understand several key statements of ECMAScript

Source: Internet
Author: User
Tags list of attributes

While and for

While and for are regular statements, whether in JavaScript or C or other programming languages. And in programming we are more commonly used, it is for for more flexible and easier to use, so some people may have a misunderstanding:

forwhilemore powerful than anything that can be while done.

In fact, we think a little bit about while for The syntax of the sentence, and we'll find:

whileLoops that cannot be looped for can not be done.

This is because for loops simply while focus the loop-related code together, but in some cases it is easier to use loops while than to use loops for . This is also the pros and cons of their respective duties.

For another point of the loop, let's look at a piece of code first:

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

Print out in the Loop i , the printout is 5 .

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

Switch statement

In other programming languages, such as C, switch statements can use numbers only, whereas in ECMAScript, a switch statement may use any data type, such as a string or an object.

Here's one thing to note: The switch equality operator used when comparing statements, that is === , so ‘10‘ and 10 not equal, because the type conversion does not occur at the time of the equality comparison.

for-in statements

The for-in statement is an iterative statement of precision that can be used to iterate over the properties of an object and, of course, to iterate over the properties of an algebraic group. The following examples illustrate:

For-in traversing objects
    • Window

A special object is traversed first window :

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

Will print a long and long list of attributes, which can be viewed by themselves, not listed here.

    • Custom Objects

Traverse 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

Iterating through 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 scopes, that is, you can set the scope of the code to a specific object. As follows:

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

These two sentences are obtained hostname separately url , because they are shared location (properties under the same object), so we can constrain the scope to the location object by using a with statement location . As follows:

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

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

Label statement

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

Its syntax is:

label: statement

The following is a detailed look at label the use of tags.

1, first give a section of the 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: break jumping out of the inside for Loop, the j remaining 5 cycles are no longer executed, so the result of printing is 95 .

2, Next we will break replace 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);

Description: continue jump out of this cycle, that is, out of the loop for in the internal loop, so the result is printed 99 .

3. Next we add a outer label labeled label and look at the print results separately:

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);

Description: After adding the label, with the break jump to the label, that is, the program jumped out of the outer outer loop, that is, the program executes to i=5 and j=5 stop execution, so the result is printed 55 .

4, we can change to continue 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 with continue , so when the program executes and i=5 j=5 does not jump out of the outer loop, but just jump out of the inner loop, that the rest of the 5 time is not executed, so the result is printed 95 .

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

Understanding several key statements of ECMAScript

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.